aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to parse XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to parse XML" Watch "How to parse XML" New topic
Author

How to parse XML

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Sep 10, 2001
Posts: 285
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Sep 10, 2001
Posts: 285
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to parse XML