JSP XML with DOM

Joined
Aug 12, 2007
Messages
1
Reaction score
0
Hi all.

I am new to JSP and DOM, I am trying to get a subset of values (categories) from an xml file which looks something like this:

<page>
<something>
...
</something>
<categories>
<category>cat1</category>
<category>cat2</category>
</categories>
</page>

So far I have come up with this. It returns "null null null", which is interesting because there are only two categories in my xml file.. Any help would be greatly appreciated.

Regards,
John.

Document doc = db.parse(file);
NodeList list = doc.getElementsByTagName("categories");
for (int i=0; i<list.getLength(); i++) {
Element element = (Element)list.item(i);
NodeList childNodes = element.getChildNodes();
if (childNodes != null) {
for (int x=0; x<childNodes.getLength(); x++) {
String string = childNodes.item(x).getNodeValue();
out.println(string);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top