Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Parse Input Stream into DOM object.

I'm attempting to create an XML DOM document from an Input Stream using the following code.  The request object if HttpServletRequest request object on a doPost call.

InputStream input = request.getInputStream();
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.parse(input);

The last line of code gets this exception:  org.xml.sax.SAXParseException: Content is not allowed in prolog.

On the post side, I've created a valid XML string that is UTF-8 encoded.  Do I need to decode first before the docBuilder.parse(input) call?  

Can anyone help me out?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

whats the code sending the post look like?
Avatar of HLRosenberger

ASKER

If I do the decode first then I'm OK.  No exception.

Now another question:  I have a document as a result of this line of code

Document doc = docBuilder.parse(input);

How do I iterate through the elements in the document?  So far, every attempt I have made results in EXCEPTION: java.lang.UnsupportedOperationException: Not yet implemented

I'm using this code:
NodeList nodeList = doc.getElementsByTagName("parms");
    for(int i=0; i<nodeList.getLength(); i++){
        Node childNode = nodeList.item(i);
        Log_Message("XXXXXX = " + childNode.getNodeValue());
    }

I get this exception trying to excute
1) childNode.getTextContent(); OR
2) childNode.toString(); OR
3) childNode.getNodeValue()
> If I do the decode first then I'm OK.

how exactly are you 'decoding' it?
Here's the decode code:

       // Get the input string for the POST request object.
        InputStreamReader input = new InputStreamReader(request.getInputStream());

        // Get the length of the data, then create a buffer of that
        // size to handle it, then read the data that was POSTed.
        int read_len = request.getContentLength();
        char[] cbuf = new char[read_len];
        input.read(cbuf);

        // Convert it to a string and decode it.
        String Encoded_XML = new String(cbuf);
        String XML = URLDecoder.decode(Encoded_XML, "UTF-8");

are you url encoding it before sending?
yes. I have the problem fixed. Thanks!
what was it?
Sorry. The decoding.  I was not decoding it.  UTF-8.   I decode and everything is fine.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why?   I thought I would need to.  Or is it that XML by definition does not need to be encoded because it's all ASCII.

Thanks!  
only needs to be encoded when using GET (as it needs to go into the url)
You can pass whatever you want in a POST
OK.  Thanks.  Why is there a POST and a GET?  What's the main differences?
Yes.  Thanks. No encoding/decoding works fine!!