• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to parse XML

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do a GET to a Servlet from some Java code and I get back this one line:
<REQUEST TIMESTAMP="2004-01-15 15:38:02.153" TotalDevices="0"/>
I do not know how to parse that using XML. I realize that I could just treat it like a string in order to get what I needed. I really do nto know where to start. This is what I tried:
DOMParser parser = new DOMParser();
parser.parse(xml); //xml var equals that line above
Document doc = parser.getDocument();
System.out.println(doc);
Noteworthy import statements:
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
It actually blows up at the parser.parse. It says:
java.net.MalformedURLException: no protocol: WHy? I dont know.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parse(String) javadoc would've told you that the String you're passing is expected to be a URI, not the actual XML content. Anyway, you should take a look at StringToDocument at our lovely JavaRanch Wiki.
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that and when I do a:
System.out.println(doc);
or
System.out.println(doc.getDoctype());
I get null.

This is what my code look likes:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
*************
My xml var equals this:
<REQUEST TIMESTAMP="2004-01-16 09:03:34.597" TotalDevices="0"/>
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. No exceptions?
The reason why it's failing might be that your XML string does not constitute a valid document but only a fragment (it's missing the XML declaration which is mandatory according to the spec), but I have no idea why you're getting a null instead of an exception (that is, if you're not getting an exception)...
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still stuck.
I am trying to parse and I get:
[Fatal Error] :1:2133: The markup in the document following the root element must be well-formed.
org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
If I add this line to the beginning of the xml I get null not error:
<?xml version='1.0' encoding='UTF-8'?>
The code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
System.out.println(doc);
System.out.println(doc.getDoctype() );
xml var equals the following:

<REQUEST TIMESTAMP="2004-01-20 09:24:54.485" TotalDevices="31">
<DEVICE Key="396" SerialNum="00104002EC9C12600300630"/>
<DEVICE Key="397" SerialNum="00104002ECAE13300300199"/>
<DEVICE Key="398" SerialNum="00104002ECAE13300300199"/>
<DEVICE Key="399" SerialNum="00104002ECB413300300205"/>
<DEVICE Key="400" SerialNum="00104003011613300300204"/>
<DEVICE Key="401" SerialNum="00104003011613300300204"/>
<DEVICE Key="402" SerialNum="00104003011B13300300195"/>
<DEVICE Key="403" SerialNum="00104003011B13300300195"/>
<DEVICE Key="404" SerialNum="00104003609513300300187"/>
<DEVICE Key="405" SerialNum="73513FBF500000831A7286687D090083"/>
<DEVICE Key="406" SerialNum="73513FBF500000831A72A228D5070073"/>
<DEVICE Key="407" SerialNum="73513FBF500000831A72A228D5070073"/>
<DEVICE Key="408" SerialNum="73513FBF500000831A955908D60D00C4"/>
<DEVICE Key="409" SerialNum="73513FBF500000831AA24267A7000000"/>
<DEVICE Key="410" SerialNum="73513FBF500000831AA24267A7000000"/>
<DEVICE Key="411" SerialNum="73513FBF500000831AA957B1C9000090"/>
<DEVICE Key="412" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="413" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="414" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="415" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="416" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="417" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="418" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="419" SerialNum="73513FBF500000831AAA2AA5AB070066"/>
<DEVICE Key="420" SerialNum="EMULATOR"/>
<DEVICE Key="421" SerialNum="EMULATOR"/>
<DEVICE Key="422" SerialNum="EMULATOR"/>
<DEVICE Key="423" SerialNum="EMULATOR"/>
<DEVICE Key="424" SerialNum="EMULATOR"/>
<DEVICE Key="425" SerialNum="EMULATOR"/>
<DEVICE Key="426" SerialNum="EMULATOR"/>
</REQUEST>
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying to solve this kind of thing I like to get more out of the SAX parse exception - code like the following will give you a line number and column number.

Bill
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I got:
[Fatal Error] :1:2161: The markup in the document following the root element mus
t be well-formed.
org.xml.sax.SAXParseException: The markup in the document following the root ele
ment must be well-formed.
Line number: 1
Column number: 2161
Public ID: null
System ID: null
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic