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:

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.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

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

No, I will be using it as a JAVA Mapping as I have HTTP adapter on the sender. If I have to write an Adapter Module do I have to change the code?

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

Not the Data, I am trying to validate if the incoming XML is as per the stored XSD.

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

Yes I know, I can use the XML validation function of SAP PI 7.1.1, but the problem is that we have to send a Validation Acknowledgement back to sender if the file is not as per the available DTD.

Thank you,

Pankaj.

Former Member
0 Kudos

Hi Pankaj,

Ok if i not understood ur req correctly, correct me..

U Have sender HTTP and u need to validate the incoming XML with XSD. If not fine then u need to send an acknowledgement back.

Yes u can achive this using a Java Mapping class..

But plese note that there is no Main function in java mappping class , if u use SAX or DOM Parser,

U should use execute method...

Please just search in SDN u will see a lot of materials, if u cannot figure out, do post..

Babu

Former Member
0 Kudos

Hi,

The JAVA code which I posted, is working fine in NWDS. My requirement is to Determine the receiver accordingly as I will have to use Enhanced receiver Determination.

What should be added in the JAVA code so that it populates the below structure if validation fails:

<Receivers>

<Receiver>

<Party>

</agency>

</scheme>

</Service>

</Receiver

</Receivers>

If no validation error, Service should be Service1 and pass the XML as it is

If validation error, Party should be Party1 and Service should be Service2 and send error details to the Service2

Thank you,

Pankaj.

Former Member
0 Kudos

Hi Pankaj,

Yes u r correct. It will generate the o/p correctly in NWDS. In face thats the way to test remotely.

If u want to import the archive Java mapping and make itto work, this will not work....

Please search by java mapping in SDN

Also the target XML u mentioned, can be achived in Java mapping. U put it in catch Block of Java Mapping

Babu

Former Member
0 Kudos

Hi,

Also the target XML u mentioned, can be achived in Java mapping. U put it in catch Block of Java Mapping

How to make it work in SAP PI7.1.1? I really have no idea about it. If possible can you please provide me with the updated code which will work in PI?

Thank you,

Pankaj.

Former Member
0 Kudos

Dear Pankaj,

Writing a code for a Java Mapping and giving it to you,... is really next to impossible to any one in this forum..

Please search in SDN, U will get lot of materials.

check at Java Mapping blogs in

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=79955426

If you find any difficulty please do post

@ Pankaj:

If u r using 7.1 there in one more possibility.

In sender agreement choose Validation by Adapter.

Since your source is HTTP , wrks in Sync , it will automatically send the Error Acknowledgement to sender... Just check once..

Babu

Edited by: hlbabu123 on Oct 1, 2010 12:45 PM

Edited by: hlbabu123 on Oct 1, 2010 1:50 PM

Former Member
0 Kudos

Hi,

I cannot use the XML validation feature of SAP PI 7.1.1 because the sender is expecting the message in a pre-defined format.

Since your source is HTTP , wrks in Sync , it will automatically send the Error Acknowledgement to sender

Does this mean that error details cannot be send to sender using XML validation feature if the adapter is JMS or FILE?

Thank you,

Pankaj.

Former Member
0 Kudos

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