Baconbitz
Baconbitz

Reputation: 149

Retrieving information from an XML file using DOM in Java

I have been struggling on this for quite a while.I'm trying to figure out how to get "Crime, Drama, and Thriller. from category any category using DOM for Java. I'm also having trouble understanding how I should dive into this. (for Instance, should I pull categories out first, then reference each category as a child, or reference immediately by attribute.

<movie>
<homepage>http://www.sincitythemovie.com/<;/homepage>
<trailer>http://www.youtube.com/watch?v=80<;/trailer>
<categories>
<category type="genre" name="Crime" url="http://themoviedb.org/genre/crime" id="80"/>
<category type="genre" name="Drama" url="http://themoviedb.org/genre/drama" id="18"/>
<category type="genre" name="Thriller" url="http://themoviedb.org/genre/thriller" id="53"/>
</categories>
</movie>

This is what I've been using for < open > somthing < /open >

private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}

Upvotes: 0

Views: 115

Answers (2)

Sunil
Sunil

Reputation: 1248

You can reference immediately by attribute. e1.getAttribute("name") will give you the attribute of the category tag.

Upvotes: 1

Zaz Gmy
Zaz Gmy

Reputation: 4356

use

 textVal= e1.getAttribute("name");

Upvotes: 1

Related Questions