cancel
Showing results for 
Search instead for 
Did you mean: 

Exception in Java Mapping

Former Member
0 Kudos

hi All,

I'm trying to validate the incoming XML against a given XSD using Java Mapping.. When i execute it i get the following error in the Mapping...

com.sap.engine.lib.xml.util.NestedException -> com.sap.engine.lib.xml.parser.ParserException -> java.io.IOException: Parsing an empty source. Root element expected!

Please do help me out..... I have given my java Code Below.....

Note:The XSD is attached in the same zip file which contains java class and the zip file is imported in the Imported ARchive Area....

package cts.com.XSD;

import java.io.*;

import java.util.Map;

import com.sap.aii.mapping.api.*;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class TestJavaMapping implements StreamTransformation

{

Map param=null;

public void setParameter(Map arg0)

{

this.param=arg0;

}

public void execute(InputStream in, OutputStream out) throws StreamTransformationException

{

byte b[];

byte b1[];

try

{

b = new byte[in.available()];

in.read(b);

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

InputStream Schemainput= new

TestJavaMapping().getClass().getClassLoader().getResourceAsStream("cts/com/XSD/ShipmentDetails.xsd");

b1 = new byte[Schemainput.available()];

Schemainput.read(b1);

String XsdString =new String(b1);

trace.addInfo("XSD obtained");

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

docBuilderFactory.setNamespaceAware(true);

docBuilderFactory.setValidating(true);

trace.addInfo("After Setting True");

docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema");

docBuilderFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource",XsdString);

trace.addInfo("After Setting ATtribute");

trace.addInfo("B4 Parsing");

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document validDoc = docBuilder.parse(in);

trace.addInfo("After Parsing");

out.write(b);

}

catch(Exception e)

{

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo(e.getMessage());

}

}

}

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Sundararamaprasad,

When you get to

docBuilder.parse(in)

you have already read all available bytes from the InputStream near the beginning of the execute method:

in.read(b)

Either create a new ByteArrayInputStream from b and pass it to

docBuilder.parse()

or implement a so-called TeeInputStream that in addition to returning the bytes read from it, also writes them to an OutputStream (in your case out). For large documents, the latter should be more efficient than reading the entire document into memory.

While we're talking efficiency, since you are only trying to validate the XML (as opposed to processing its contents) SAX parsing is preferable to DOM parsing.

Best regards,

Thorsten

PS Check out my colleague <a href="/people/morten.wittrock/blog/2006/03/21/validating-messages-in-xi-using-xml-schema">Morten's blog</a> on this topic for additional pointers.

Answers (2)

Answers (2)

Former Member
0 Kudos

hello Sundararamaprasad,

could you solve the problem with the "generic exception"? i have the same problem at validating incoming xml messages. i do not know the fault!!

can you help me? please post or email me: alexander.schramm@warp-it.com

thanks a lot

ciao alex

Former Member
0 Kudos

Hi Alex,

Even now i dont know the solution for it. If i find a solution sure i'll post it and complete the thread.If any one finds the solution kindly post the solution in the thread.

Regards,

Sundararamaprasad.

Former Member
0 Kudos

hi sundararamaprasad,

i think i have found a small solution for our problem. i put the xsd in the jar file where the java program is in it. i will not reference to a directory where the xsds are in it.

now here a small part of coding:


DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setValidating(true); 

documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
InputStream mySchema = this.getClass().getClassLoader().getResourceAsStream("your.xsd");
documentBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", mySchema);

DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
  		docBuilder.setErrorHandler(new yourErrorhandler());
  		Document doc = docBuilder.parse(in);

my problem was that the xsd includes another xsds so the parser does not find the include xsds although the xsds were put into the jar file.

i hope you can work with that.

bye

alex

Former Member
0 Kudos

Hi All,

The reason for the Generic Exception has been found out....Let me explain it using a code sample

byte b1[]=new byte[in.available()];

in.read(b1);

Previously I've Been using the below code

<i>ByteArrayInputStream bais=new ByteArrayInputStream(b1);

docBuilderFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource",bais);</i>

After changing the above code as the one given below The Generic Exception has not Come and the scenario is through....

<b>docBuilderFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource",new ByteArrayInputStream(b1));</b>

Regards,

Sundararamaprasad

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

Thats strange.. Thanks for the update..!!

Regards,

Bhavesh

Former Member
0 Kudos

Hi Thorsten,

Thanks for the Suggestion of using SAX instead of DOM... i even tried using SAX parser i get "Generic Exception"..i have give the code below.. please do help me out..

package cts.com.XSD;

import java.util.Map;

import java.io.*;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationConstants;

import com.sap.aii.mapping.api.StreamTransformationException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;

import org.xml.sax.helpers.DefaultHandler;

import org.w3c.dom.Document;

public class TestJavaMapping implements StreamTransformation

{

Map param=null;

public void setParameter(Map arg0)

{

this.param=arg0;

}

public void execute(InputStream in, OutputStream out) throws StreamTransformationException

{

try

{

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setNamespaceAware( true);

factory.setValidating( true);

InputStream Schemainput = new

TestJavaMapping().getClass().getClassLoader().getResourceAsStream("cts/com/XSD/ShipmentDetails.xsd");

SAXParser parser = factory.newSAXParser();

parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",

"http://www.w3.org/2001/XMLSchema");

parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource",Schemainput);

byte b[]=new byte[in.available()];

in.read(b);

XMLReader reader = parser.getXMLReader();

reader.parse(new String(b));

out.write(b);

}

catch ( ParserConfigurationException e)

{

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo(e.getMessage());

}

catch ( SAXException e)

{

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo(e.getMessage());

}

catch ( IOException e)

{

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo(e.getMessage());

}

catch(Exception e)

{

AbstractTrace trace=(AbstractTrace)param.get(StreamTransformationConstants.MAPPING_TRACE);

trace.addInfo(e.getMessage());

}

}

}

0 Kudos

Try posting the exception and stack trace.

- Thorsten