Java5 Xpath problem

M

Moiristo

I am trying to evaluate an XML document using the javax.xml libraries,
but I guess I'm doing something wrong. I need to get all 'layer'
attributes from the document. To do this, I did this:

NodeList layers = (NodeList) path.evaluate("/root//layer",
<xml inputsource>, XPathConstants.NODESET);

Then, I need to use the elements within that layer. The code I use is:

for(int i = 0;i<layers.getLength();i++){
Node layer = layers.item(i);
lname = layer.getNextSibling().getNodeValue().trim();
lsource = layer.getNextSibling().getNodeValue().trim();
ldest = layer.getNextSibling().getNodeValue().trim();
}

It finds the layers within the document, but when I store the element
values in the for-loop, it stores empty strings (when I call
getNodeName(), it returns "#text"). Do I need to evaluate the expression
differently, because it seems like the elements within the layers aren't
evaluated.

As an example, a layer node:

<layer>
<name>
Name
</name>
<sourceTable>
Source
</sourceTable>
<destColumn>
gcolName
</destColumn>
</layer>

TIA.
 
O

Oliver Wong

Moiristo said:
I am trying to evaluate an XML document using the javax.xml libraries,

You're actually evaluating an XPath on an XML document, not the XML
document itself.
but I guess I'm doing something wrong. I need to get all 'layer'
attributes from the document.

"layer" is an element, not an attribute.
To do this, I did this:

NodeList layers = (NodeList) path.evaluate("/root//layer",
<xml inputsource>, XPathConstants.NODESET);

Then, I need to use the elements within that layer. The code I use is:

for(int i = 0;i<layers.getLength();i++){
Node layer = layers.item(i);
lname = layer.getNextSibling().getNodeValue().trim();
lsource = layer.getNextSibling().getNodeValue().trim();
ldest = layer.getNextSibling().getNodeValue().trim();
}

It finds the layers within the document, but when I store the element
values in the for-loop, it stores empty strings (when I call
getNodeName(), it returns "#text"). Do I need to evaluate the expression
differently, because it seems like the elements within the layers aren't
evaluated.

As an example, a layer node:
<layer>
<name>
Name
</name>
<sourceTable>
Source
</sourceTable>
<destColumn>
gcolName
</destColumn>
</layer>

layer.getNextSibling(), which you call 3 times, will always return the
same node, and I don't think it's the node you want anyway. See
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getChildNodes()

- Oliver
 
M

Moiristo

Oliver said:
You're actually evaluating an XPath on an XML document, not the XML
document itself.
"layer" is an element, not an attribute.

You're right, sorry for my bad terminology :) I had a rough weekend..
layer.getNextSibling(), which you call 3 times, will always return
the same node, and I don't think it's the node you want anyway. See
http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getChildNodes()

Hmm, RTFM you say? You're right, I thought the method just advanced to
the next node when I called it. Fixed it now :) thnx!
 
M

Martin Honnen

Moiristo wrote:

NodeList layers = (NodeList) path.evaluate("/root//layer",
<xml inputsource>, XPathConstants.NODESET);

Then, I need to use the elements within that layer. The code I use is:

for(int i = 0;i<layers.getLength();i++){
Node layer = layers.item(i);
lname = layer.getNextSibling().getNodeValue().trim();

You can use e.g.
lname = layer.getElementsByTagName("name").item(0);
Or you can use XPath again e.g.
lname = (Node) path.evaluate("name", layer, XPathConstants.NODE);
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top