Simple XPATHs arent working... HELP!

G

gfrommer

Hello Everyone,

I am having some very strange results using the XPath classes
included with the JDK1.5.0_01. Check this out:

XML:

<final>
<a>
<b>1</b>
<b>2</b>
</a>
<a>
<b>3</b>
<b>4</b>
</a>
</final>

Code:

DocumentBuilder db =
DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document impDoc = db.parse(impFile);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xpath.evaluate(q, impDoc,
XPathConstants.NODESET);

printNodes(nl);

public void printNodes(NodeList nl) {
if(nl == null) { return; }
if(nl.getLength() == 0) { return; }

for(int i=0; i<nl.getLength(); i++) {
Node n = nl.item(i);

if(n.getNodeValue() != null &&
n.getNodeValue().toString().trim().length() > 0)
System.out.println(n.getNodeValue());

NodeList nl2 = n.getChildNodes();
printNodes(nl2);
}

return;
}


--------------------------------

When I run the XPAth query: //b[.=2] That returns the proper <b> node.
But if I do //b[.>2] or //b[b >= 1] or //b[. != 1] .... all of those
return nothing even though there are valid results. Whenever I compare
on a string it always returns nothing. Are the XPath classes in the
JDK1.5 buggy? Should I be using a different library?

Im really confused.... simple xPaths without any []'s always seem to
work, but the node tests just dont want to work.

Any ideas?
 
T

Tjerk Wolterink

Hello Everyone,

I am having some very strange results using the XPath classes
included with the JDK1.5.0_01. Check this out:

XML:

<final>
<a>
<b>1</b>
<b>2</b>
</a>
<a>
<b>3</b>
<b>4</b>
</a>
</final>

Code:

DocumentBuilder db =
DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document impDoc = db.parse(impFile);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xpath.evaluate(q, impDoc,
XPathConstants.NODESET);

printNodes(nl);

public void printNodes(NodeList nl) {
if(nl == null) { return; }
if(nl.getLength() == 0) { return; }

for(int i=0; i<nl.getLength(); i++) {
Node n = nl.item(i);

if(n.getNodeValue() != null &&
n.getNodeValue().toString().trim().length() > 0)
System.out.println(n.getNodeValue());

NodeList nl2 = n.getChildNodes();
printNodes(nl2);
}

return;
}
en < or unkown in xpath, you should use &lt; and &gt;
For != im not sure, maye not(.=1) works?
return nothing even though there are valid results. Whenever I compare
on a string it always returns nothing. Are the XPath classes in the
JDK1.5 buggy? Should I be using a different library?

Im really confused.... simple xPaths without any []'s always seem to
work, but the node tests just dont want to work.

Any ideas?
 
G

gfrommer

I tried it with &lt; and &gt; and both dont work. They each throw an
"javax.xml.transform.TransformerException: Expected ], but found: &lt;"
error.
Why are these simple xpaths not working? There isnt much to go wrong in
the code, it has to be Java's XPath engine.
 
G

gfrommer

Also, when I run the XPath with <= it simply returns nothing... when I
use the &lt; or &gt; its throwing an exception. Very confusing.
 
G

gfrommer

Hmph.... html is interpreting the < and > with those symbols you
suggested.... thats what I tried...
 
G

gfrommer

I figured it out... the DOS window was misinterpreting my command line
parameters. I like how I immediatly faulted Java's XPath engine
though.... it's not my problem, the universe is wrong :)

Peace
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top