File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Servlet with XML and getInputStream() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Servlet with XML and getInputStream()" Watch "Servlet with XML and getInputStream()" New topic
Author

Servlet with XML and getInputStream()

JW Boom
Greenhorn

Joined: Jan 05, 2009
Posts: 3
I have a servlet that is receiving a small amount of XML data. The following code works but if I have bad XML data and throw an exception I want to still be able to read the data and get the correlation tag data.

Any help would be GREAT!!

<?xml version=\"1.0\"?>
<request>
<correlation>001</correlation>
<data>some data 1234567890</data>
</request>"

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

PrintWriter out = response.getWriter();

try {
// Get the Input Stream
InputStream is = request.getInputStream();

// Create a Factory to Obtain DOM Parser
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Creates a Parser with configured values from jaxp.properties
DocumentBuilder builder = factory.newDocumentBuilder();

// Pass the InputStream and Construct the Document
Document doc = builder.parse(is);

// Get the ROOT element
Element docEle = doc.getDocumentElement();

//Got the Document reference to modify,view or play around with.

catch(SAXException se)
{
se.printStackTrace();
out.println("<error>XML</error>");
}
catch(IOException ioe)
{
ioe.printStackTrace();
}


Thanks, jwrab
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
It is hard to help you without understanding the details.
Please read this: http://faq.javaranch.com/java/TellTheDetails
JW Boom
Greenhorn

Joined: Jan 05, 2009
Posts: 3
Bauke Scholtz wrote:It is hard to help you without understanding the details.
Please read this: http://faq.javaranch.com/java/TellTheDetails


More info:

When I make the following call to parse the malformed XML data it throws and XML exception.

// Pass the InputStream and Construct the Document
Document doc = builder.parse(is);

Error Message:
[Fatal Error] :1:232: XML document structures must start and end within the same entity.
org.xml.sax.SAXParseException: XML document structures must start and end within the same entity.

The JDK that is used is : jdk1.5.0_15

As I am kind of new with this if I need to add more please let me know what I should add. Thanks jwrab
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Your XML is simply incorrect. Ensure that the document is well formed XML. The actual problem is that the tags aren´t nested properly, eg: <a><b></a></b> instead of <a><b></b></a>. Fix it accordingly.
JW Boom
Greenhorn

Joined: Jan 05, 2009
Posts: 3
Bauke Scholtz wrote:Your XML is simply incorrect. Ensure that the document is well formed XML. The actual problem is that the tags aren´t nested properly, eg: <a><b></a></b> instead of <a><b></b></a>. Fix it accordingly.


My real question is if I get a message with malformed XML using:

request.getInputStream();
Document doc = builder.parse(is);

how can I re-read the input data such that I can send back the <correlation>001</correlation> tag?

Sorry if I have not been specific enough. Thank jwrab
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

If you want to get the XML data up to the error, you will need to use a different style of parser directly, like SAX or one of the streaming XML parsers. DocumentBuilder doesn’t let you do this.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
how can I re-read the input data


First, read the input stream into a buffer, once you have the whole thing in memory you can use it repeatedly.

The standard Java library provides the ByteArrayOutputStream which you can use to collect the entire input, then get the resulting byte[].

Bill
 
jQuery in Action, 2nd edition
 
subject: Servlet with XML and getInputStream()
 
Similar Threads
converting XML with XSLT
DocumentBuilder.parse not working for String input
Parsing WBXML POSt Request Data
Problem with using InputSource in SAXParser
DOM (xml) question