HLRosenberger
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.new Instance() ;
DocumentBuilder docBuilder = dbfac.newDocumentBuilder() ;
Document doc = docBuilder.parse(input);
The last line of code gets this exception: org.xml.sax.SAXParseExcept ion: 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?
InputStream input = request.getInputStream();
DocumentBuilderFactory dbfac = DocumentBuilderFactory.new
DocumentBuilder docBuilder = dbfac.newDocumentBuilder()
Document doc = docBuilder.parse(input);
The last line of code gets this exception: org.xml.sax.SAXParseExcept
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?
whats the code sending the post look like?
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.UnsupportedOpera tionExcept ion: 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()
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.UnsupportedOpera
I'm using this code:
NodeList nodeList = doc.getElementsByTagName("
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()
2) childNode.toString(); OR
3) childNode.getNodeValue()
> If I do the decode first then I'm OK.
how exactly are you 'decoding' it?
how exactly are you 'decoding' it?
ASKER
Here's the decode code:
// Get the input string for the POST request object.
InputStreamReader input = new InputStreamReader(request. getInputSt ream());
// 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");
// Get the input string for the POST request object.
InputStreamReader input = new InputStreamReader(request.
// 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_
are you url encoding it before sending?
ASKER
yes. I have the problem fixed. Thanks!
what was it?
ASKER
Sorry. The decoding. I was not decoding it. UTF-8. I decode and everything is fine.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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
You can pass whatever you want in a POST
ASKER
OK. Thanks. Why is there a POST and a GET? What's the main differences?
ASKER
Yes. Thanks. No encoding/decoding works fine!!