Avatar of HLRosenberger
HLRosenberger
Flag 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?
JavaWeb Components

Avatar of undefined
Last Comment
HLRosenberger

8/22/2022 - Mon
Mick Barry

whats the code sending the post look like?
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()
Mick Barry

> If I do the decode first then I'm OK.

how exactly are you 'decoding' it?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
HLRosenberger

ASKER
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");

Mick Barry

are you url encoding it before sending?
HLRosenberger

ASKER
yes. I have the problem fixed. Thanks!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mick Barry

what was it?
HLRosenberger

ASKER
Sorry. The decoding.  I was not decoding it.  UTF-8.   I decode and everything is fine.
ASKER CERTIFIED SOLUTION
Mick Barry

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HLRosenberger

ASKER
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!  
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mick Barry

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
HLRosenberger

ASKER
OK.  Thanks.  Why is there a POST and a GET?  What's the main differences?
HLRosenberger

ASKER
Yes.  Thanks. No encoding/decoding works fine!!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.