• Advertisement
Sign in to follow this  

[java] DOM Parser with Java

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all i have a little problem with DOM Parser in a java program. It would be great if anyone can help me immediately with this. Description: I have a very simple XML given below. My main requirement is to get the value of the element from the xml. I am using DOM parser. When I run this program, it is outputting "null" for the node value. I might have gone wrong somewhere. Kindly help... The xml and the java source code are given below: The XML: <?xml version="1.0"?> <message-rules><first-name>Harry</first-name><last-name>Potter</last-name><eid>12345</eid><zip>12404</company-zip></message-rules> The java source: import org.w3c.dom.*; import javax.xml.parsers.*; public class TryDom { public static void main(String[] ag) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document doc = builder.parse("MsgRules.xml"); Element root = doc.getDocumentElement(); NodeList elementList = root.getChildNodes(); System.out.println(elementList.getLength()); for(int i=0;i<elementList.getLength();i++) { Node e = (Element) elementList.item(i); System.out.println(e.getNodeName()+": "+e.getNodeValue()); } } catch (Exception e) { System.out.println(e); } } }

Share this post


Link to post
Share on other sites
Advertisement
Hey

First there is a error in the xml <zip> tag ends with <company-zip>

Otherwise follw this tutorial and you should be fine:
link

Lizard

Share this post


Link to post
Share on other sites
Hi Lizard

sorry i made a mistake while posting the problem. i had used the correct xml. i have already taken the tutorial on totheriver.com.

Can you please suggest any possible programmatical error/modification?

Thanks
Hari.

Share this post


Link to post
Share on other sites
Well...do it like they do it in thier tutorial, work for me when I ahd to parse some xml in java. That is use getElementByTagName() function on elements instead of getChildNodes().

I would also rather write the xml as:

<persons>
<person>
<firstname>Harry</firstname>
<lastname>Potter</lastname>
etc..
</person>
</persons>

Share this post


Link to post
Share on other sites
I wrote my xml stuff using this:

http://www.java-tips.org/java-se-tips/org.xml.sax/extracting-attribute-values-from-xml-elements.html

Not sure if this helps though ;)

Share this post


Link to post
Share on other sites
Hi Lizard

thanks very much for responding very early. This is my first on a technical forum. Really great experience!

I actually got a solution to my problem from (http://www.onjava.com/pub/a/onjava/excerpt/learnjava_23/index3.html). My XML doesnt contain elements with same name...so getElementsByTagName() doesnt suit my requirement.

String value = org.apache.xercer.util.DOMUtil.getChildText(elementname);

The above solves my problem. Works well with getChildNodes().

Thanks very much.

Share this post


Link to post
Share on other sites

If you're doing a lot of this sort of thing you might want to consider using JAXB or XMLBeans instead of all that awkward DOM/SAX code. I do up schemas for all my XML documents (config/setup/object storage and the like) and then just run JAXB over them to produce a bunch of Java representations of the XML documents. Then just marshall and unmarshal away ! I find its more convenient than the above, to change any of the XML stuff all I have to do is make changes in the schemas. I have the object generation built into my ant build so its pretty transparent. At the moment I'm using JAXB, but it has a few drawbacks, namely you have to ship the entire JWSDP with JAXB if you're distributing it (license) and you have to use Java 1.5. I'm thinking of switching to XMLBeans which has neither of those conditions (or Castor is another alternative)

D.

Share this post


Link to post
Share on other sites
Actually I would go with JDOM on this one. The code is really well organized and documented and the FAQs are good too. Reading in XML is pretty easy. And they've gone through the trouble of creating a pretty printer format. And JDOM uses mostly java.util data structures.


import org.jdom.JDOMException;
import org.jdom.Document;

import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.IOException;

public class Main {

public static void main(String[] args) {
new Main().readTree(args[0]);
}

public final String XERCES_NO_VALIDATE = "http://apache.org/xml/"
+ "features/nonvalidating/load-external-dtd";

// To read in the file and create a multi-way tree:
protected Document readTree(String file) {
Document doc = null;

try {
SAXBuilder builder = new SAXBuilder();

builder.setFeature(XERCES_NO_VALIDATE, false);

doc = builder.build(new File(file));

} catch (JDOMException j) {
j.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}

return doc;
}

// Then to output the tree/document which JDOM builds:
protected void outputDocument(Document doc) {
try {
new XMLOutputter(Format.getPrettyFormat()).output(doc, System.out);
} catch (IOException io) {
io.printStackTrace();
}
}
}





And after that just write some recursive functions to search for the info you want.

L-

Share this post


Link to post
Share on other sites
Isn't JDOM code already part of the Java SE by now?

The org.w3c.dom package and subpackages...

I could be wrong, but it does the same thing. I used JDOM before too, great API, but ceased using it in favor of these packages.

Rodrigo

[Edit: Doh! Didn't read the original post entirely =]

Share this post


Link to post
Share on other sites
Nope, AFAIK the guys who wrote JDOM decided to foregoe the JRC because it was just too much of a hassle so it never made it into SE.

As far as parsers go Xerces is the XML parser shipped with SE. The tree built by the org.w3c.* is really meant to be language independent so there are a bunch of really strange calls, and little use of standard java.util data structures.

IMHO JDOM is better than the org.w3c.dom. The w3c has things like NodeList, DomStringList, etc which are hard to grasp as to their full analogy with XML and do not implement the List interface. While JDOM uses most the List interface heavily, and is a smaller API with direct analogy.

Its nice that the org.w3c.dom does come with the SE though.

L-

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement