I am trying to send XML documents between 2 servlets. The first servlet creates a DOM tree. A String representation of the DOM tree is obtained. Then using the URLConnection the string(containing the XML document) is posted to another servlet. I obtain the Output stream of the target servlet and write into it.
The problem occurs when the target servlet starts parsing the XML. I get a error saying 'Document Root is missing'. Could somebody help in this regard ?
Is there any other way to do this XML transfer across servlets ? If so, let me know the details.
Thanks and appreciate your efforts.
Start by making the target servlet (assuming you can access it's source code) simply dump the entire contents of the received stream to a file. Then take a look at it.
My suspicion is that you have some characters at the beginning of your stream which are messing things up. Most probably the header line. Don't know for sure, but perhaps the parser doens't like that line when it's just reading a stream.
However, rogue characters are more likely. There is a site I download XML from using Java, and the first couple of characters are always the same two quote symbols. No one can work out where they come from (believe me we tried!) so we simply strip them off if they're there and go on our merry way.
Tony Thank you so much.
I tried the approach of dumping the file content and you know what happenned,
" When I transferred the content from Source Servlet, I encoded it and I did not decode it when receiving in the target servlet "
Simple but defenitely a land-mine to watch.
Now I have another problem. How do I get a decoded stream ?
URLDecoder.decode(java.lang.String) only supports a String for decoding and as such the DocumentBuilder.parse(InputSource) supports only inputstream.inputsources etc.
Here is the code I am using
Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
InputSource source = new InputSource(reader);
doc = db.parse(source); // where doc is the Document type.
How to get a decoded(string) into a Document ? Any thoughts.
Appreciate your efforts.
PS : what is HTH ??
Assuming you have Access to the Source Code of the Target Servlet the easiest way to pass an XML file is to create a StringBuffer, Write the XML reading the Lines delimited by the Whitespaces into the StringBuffer and then Open an ObjectOutputStream into the InputStream of the Servlet and write the StringBuffer to the Target servlet. On the Target Servlet write the StringBuffer into a Temp File and from the Temp File Recreate the FileInputStream and pass it to the Document.parse() method.
I have tried this and it works fine.
If you don't want to mess around with temporary files then look at things like ByteArrayInputStream, which does exactly what you want. You can .toString() the StringBuffer and then get the bytes for that. What that in a BAIS and then you will be able to use it in your InputSource.
It Did work and that saved me a lot of trouble with the Temp files :-)
But what is this HTH?? ;-)