JSP XML with DOM

D

designbyjohn

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);
}
}
}
 
A

Andrew Thompson

On May 28, 12:22 am, (e-mail address removed) wrote:
...
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.

The best advice I can immediately offer on this code
snippet is. When in doubt - print out.
Document doc = db.parse(file);
NodeList list = doc.getElementsByTagName("categories");
for (int i=0; i<list.getLength(); i++) {
Element element = (Element)list.item(i);
System.out.println(element);

NodeList childNodes = element.getChildNodes();
System.out.println(childNodes);

if (childNodes != null) {
for (int x=0; x<childNodes.getLength(); x++) {
System.out.println(childNodes.item(x));

String string = childNodes.item(x).getNodeValue();
out.println(string);



}
}
}

As an aside. Please indent code that is posted.
Unindented code is harder for many people to
understand.

Also note that you will generally get better help
if you can prepare an SSCCE* that demonstrates the
problem. (If you had, I would be running the code
right now).

* <http://sscce.org/>
 
D

Dave Miller

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);
}
}
}
Drill down one more level:

NodeList nl = d.getElementsByTagName("categories");
for (int i = 0;i< nl.getLength(); i++){
Node nd = nl.item(i);
if(nd != null && nd.getFirstChild() != null &&
nd.getFirstChild().getNodeValue() != null){
out.print(nd.getFirstChild().getNodeValue());
}
}
 
A

Are Nybakk

*snip*
Drill down one more level:

NodeList nl = d.getElementsByTagName("categories");
for (int i = 0;i< nl.getLength(); i++){
Node nd = nl.item(i);
if(nd != null && nd.getFirstChild() != null &&
nd.getFirstChild().getNodeValue() != null){
out.print(nd.getFirstChild().getNodeValue());
}
}

This seems so familiar. After several weeks of programming in JavaScript
I really got tired of the very clumsy DOM API. To make things simpler I
made my own abstraction. I defined an object type that had simple
methods that covered the uses I had, and it turned out there weren't
many. I defined methods that set the object's "root element" field; one
that took a XMLHttpRequest object and one that took any element. This
way I could create a new object for each step in an XML hierarchy and
use the very same methods instead of messing with childnode's
childnode's childnode and whatever.

--example--

var DOM = new XmlTree();
DOM.setRequest(request);

var categories = DOM.getElements("category");

for(var i=0; i<categories.lenght; i++) {

var categoryTree = new XmlTree();
categoryTree.setRoot(categories);

//...

}

----

I used this method to generate object-trees from xml-responses. (It was
inspired by JAX-B, however as far as I can see JavaScript can't offer
that kind of functionality what with annotations and all.) But anyway,
this method was so much cleaner than using the DOM API directly for
everything. Also, it's way more dynamical, resulting in looser dependency.

Are
 
A

Arne Vajhøj

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.
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);
}
}
}

A direct modification of your code that works is:

<%@ page language="java" import="javax.xml.parsers.*,org.w3c.dom.*" %>
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("C:/cat.xml");
NodeList list = doc.getElementsByTagName("categories");
for(int i = 0; i < list.getLength(); i++) {
Element element = (Element)list.item(i);
NodeList childNodes = element.getChildNodes();
for(int j = 0; j < childNodes.getLength(); j++) {
if(childNodes.item(j).getNodeType() == Node.ELEMENT_NODE &&
childNodes.item(j).getNodeName().equals("category")) {
Element child = (Element)childNodes.item(j);
String txt = child.getFirstChild().getNodeValue();
out.println(txt);
}
}
}
%>

But that code can be a lot simplified by using XPath:

<%@ page language="java"
import="javax.xml.parsers.*,org.w3c.dom.*,org.apache.xpath.*" %>
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("C:/cat.xml");
NodeList cats = XPathAPI.selectNodeList(doc,
"//page/categories/category/text()");
for(int i = 0; i < cats.getLength(); i++) {
String txt = cats.item(i).getNodeValue();
out.println(txt);
}
%>

And since JSTL supports XML, then it can done without scriptlet
code at all:

<%@ page language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<c:import var="xml" url="file:///C:/cat.xml"/>
<x:parse var="doc" xml="${xml}"/>
<x:forEach select="$doc//page/categories/category">
<x:eek:ut select="."/>
</x:forEach>

Arne
 
A

Arne Vajhøj

Are said:
This seems so familiar. After several weeks of programming in JavaScript
I really got tired of the very clumsy DOM API. To make things simpler I
made my own abstraction. I defined an object type that had simple
methods that covered the uses I had, and it turned out there weren't
many. I defined methods that set the object's "root element" field; one
that took a XMLHttpRequest object and one that took any element. This
way I could create a new object for each step in an XML hierarchy and
use the very same methods instead of messing with childnode's
childnode's childnode and whatever.
I used this method to generate object-trees from xml-responses. (It was
inspired by JAX-B, however as far as I can see JavaScript can't offer
that kind of functionality what with annotations and all.) But anyway,
this method was so much cleaner than using the DOM API directly for
everything. Also, it's way more dynamical, resulting in looser dependency.

There are so many XML API's available in Java - many of them are
a lot simpler than W3C DOM.

XPath can often simplify things a lot.

JDOM is more Java and less W3C.

Some like DOM4J.

Arne
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top