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?
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?