cancel
Showing results for 
Search instead for 
Did you mean: 

XML Validation using JAVA

Former Member
0 Kudos

Hi,

I have a JAVA program:

package dTDValidate;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

public class DTDValidateXML {
	public static void main(String args[]) {	
		try{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			builder.setErrorHandler(new org.xml.sax.ErrorHandler() {
				//Ignore the fatal errors
				public void fatalError(SAXParseException exception)throws SAXException { }
				//Validation errors 
				public void error(SAXParseException e)throws SAXParseException {
					System.out.println("Error at " +e.getLineNumber() + " line.");
					System.out.println(e.getMessage());
					System.exit(0);
				}
				//Show warnings
				public void warning(SAXParseException err)throws SAXParseException{
					System.out.println(err.getMessage());
					System.exit(0);
				}
			});
			Document xmlDocument = builder.parse(new FileInputStream("Employee.xml"));
			DOMSource source = new DOMSource(xmlDocument);
			StreamResult result = new StreamResult(System.out);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "Employee.dtd");
			transformer.transform(source, result);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

I have some queries about the above code:

I have output structure as:

<Receivers>

<Receiver>

<Party>

</agency>

</scheme>

</Service>

</Receiver

</Receivers>

1) What modification should be made to the above code to ensure so that the correct Receiver Service is populated by the above JAVA code as per the output.

For example:

If no validation error, Service should be Service1

If validation error, Party should be Party1 and Service should be Service2

Please help.

Thank you,

Pankaj.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Closing the Thread.