Skip to Content
avatar image
Former Member

XML Validation using JAVA

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:

1) I have to select Receiving Service based on the output of the JAVA validation code and I am using Enhanced Receiver Determination. 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 error, Receiver Service should be Service1

If error, Receiver Service should be Service2

Please help.

Thank you,

Pankaj.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Oct 01, 2010 at 06:34 AM

    Hi,

    Do u mean that u will make a Adapter Module and deploy it, and wil use for validating the XML?

    What exactly XML Validation u r tring to do, data or XML Syntax?

    Please confirm these because u can achieve the same withot code also...

    Babu

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Pankaj,

      Ok then go for Integration Engine XML Validation.

      In Java mapping evaluate the source and then if any error return the Acknowledgement accordingly.

      I prefer going for 1:2 multimapping in Java.

      Both targets/message types has occurence 0..1

      Babu