Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML Parsing - DOM Parser

880760Aug 18 2011 — edited Aug 18 2011
Examples of how to parse an XML document abound on the web. They all seem to revolve around the same bush of beginning by creating an object (typically named "doc") containing the XML document. That's fairly easy to do; and, one would think that from there it would be smooth sledding. Based on what I have read, I would think that I would then be able to assign the value of the first element in the document named "country" to the variable, "myvar" with the following statement:

String myvar = getElementsByTagName("country").item(0);

That doesn't seem to be the case and that's where things begin to get complicated. Here is the relevant segment of some test code that I have put together based on examples found on the web:
NodeList nList=doc.getElementsByTagName("country");
for (int count=0;count<nList.getLength();count++) {
Node nNode=nList.item(count);
if (nNode.getNodeType()==Node.ELEMENT_NODE) {
Element eElement=(Element)nNode;
System.out.println("Country: " + getTagValue("country",eElement));
}
}
For openers, I can't get the above code to compile. I get:
Parse.java:22: cannot find symbol
symbol : method getTagValue(java.lang.String,org.w3c.dom.Element)
location: class Parse
System.out.println("Country: " + getTagValue("country",eElement));
Secondly, I'd really like to understand what is happening in the code. Apparently we iterate over a node list; but, I just can't seem to understand what is going on inside the iteration. One thing that I am particularly confused by is that I have absolutely no idea what "Node.ELEMENT_NODE" is.

I realize that I am asking a lot. If I could just get some help with why what I have won't compile and maybe some brief comments relating to what's going on inside the interation loop it would be most helpful. Thanks for whatever I can get.

... doug
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 15 2011
Added on Aug 18 2011
5 comments
498 views