Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SAXBuilder + ServletInputStream 2

Status
Not open for further replies.

Swamphen

Programmer
Jul 17, 2001
84
0
0
IT
Dear all,

I try to build a JDOM Document with a ServletInputStream as argument, but stumble upon a JDOMParseException.

The code that produces the error is:

ServletInputStream inputStream = request.getInputStream();
SaxBuilder builder = new SaxBuilder();
builder.build(inputStream);

The errormessage I receive is:
org.jdom.input.JDOMParseException: Error on line 1: Content is not allowed in prolog.

I tried it with different XML files.

Has anyone had success with using a ServletInputStream as argument, or should I always first write the XML file to a temporary location and reread it afterwards (which seems a more inefficient way of tackling the problem).

Thanks very much,

Gert
 
In theory it should be OK as long as the only data in the stream is XML. I wuold try reading the stream yourself to check that pure XML is coming in on it.

--------------------------------------------------
Free Database Connection Pooling Software
 
I have the exact same situation and I had to read it into a string buffer before parsing it just incase I needed to see what was coming across. The times I got the error you are getting is when the producer sent a name=value pair. Example; outputStream.write("xml=" + xmlDoc); then on your side you would have an extra "xml=" on the stream so to avoid not knowing I loop around always putting it into a string buffer and then logging it if I get an exception so I can see exactly what went wrong. Hope it helps a little.

Brian


Spend like you don't need the money,
love like you've never been hurt and dance like nobody's watching!
 
Does it work if you build the entire XML from the stream into a String first, then apply the DOM parser to that String?

With a String, you could parse with something like
Code:
Document inputDoc= documentBuilder.parse(new InputSource(new StringReader(xmlString)));

This is *slightly* better than writing the XML to a file first. I recall having some DOM parsing problems with XML from a Socket and this approach fixed it. I don't like this solution (at all) but have never had time to go back and investigate it further.


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks all,

indeed it is possible to just read from the ServletInputStream.

Just as suggested, I had some extra characters preceeding the actual XML part and this caused the error.

For debugging I now read the HttpServletRequests InputStream (the ServletInputStream) at server side (in doPost() method), write each read buffer immediately back to the HttpServletResponses Outputstream and on the clients side I then read the BackEndConnections InputStream. This appears to work fine, although I'm now facing another strange problem : the entire file is uploaded (6MB) (checked via simple print statements during upload loop), but in the doPost() method only 3763 bytes can be retrieved from the HttpServletRequests InputStream. Probably it is again a quite daft problem, but just to be sure: can it be a problem of a too small default Tomcat Connector buffer size?

Thanks,

Gert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top