我有一个函数,在这个函数中,我使用Xpath获取节点的位置如下:
Node goTo;
.....
private Node xpathgo(Node node) throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
Node result;
if (node == null || node.getNodeName() == null){
return null;
}
xpathgo(node.getFirstChild());
result = (Node) xpath.evaluate("//*[. = \"" + goTo.getNodeValue() + "\"]", node,XPathConstants.NODE);
xpathgo(node.getNextSibling());
return result;
}
基本上我用它来获取一个由URL html代码制作的DOM节点,但是我遇到了两个问题,首先,有时我得到这个例外:
Exception in thread "main" java.lang.RuntimeException: Could not resolve the node to a handle
at com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault.getDTMHandleFromNode(DTMManagerDefault.java:574)
at com.sun.org.apache.xpath.internal.XPathContext.getDTMHandleFromNode(XPathContext.java:182)
at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:301)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:210)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
以及其他一些节点:
Caused by: javax.xml.transform.TransformerException: Expected ], but found: the
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(XPathParser.java:608)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(XPathParser.java:526)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(XPathParser.java:1935)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(XPathParser.java:1724)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(XPathParser.java:1624)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(XPathParser.java:1595)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(XPathParser.java:1315)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(XPathParser.java:1234)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(XPathParser.java:1140)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(XPathParser.java:1061)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(XPathParser.java:1003)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(XPathParser.java:928)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(XPathParser.java:868)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(XPathParser.java:832)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(XPathParser.java:805)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(XPathParser.java:788)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:127)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:193)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
... 7 more
但有趣的是,在展位的情况下,节点是一个[#text]
节点,让我感到困惑,为什么会发生这种情况。
答案 0 :(得分:0)
我已经创建了一个函数,可以根据需要返回节点的位置:
<强>的test.xml 强>
<html>
<div id='teste'>Teste</div>
<div id='poutine'>poutine</div>
<div id='ola'>Ola tudo ebm!</div>
</html>
XMLManager类:
public final class XMLManager {
public static Integer getPositionByNode(Node node, File filteForLookUp){
Integer position = null;
try {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(filteForLookUp);
NodeList ndList = doc.getElementsByTagName("*");
if(ndList != null){
for(int i=0;i<ndList.getLength();i++){
if(ndList.item(i).isEqualNode(node)){
position = i;
System.out.println("Dans la condition");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return position;
}
}
my Main:
public class MyMain {
/**
* @param args
*/
public static void main(String[] args) {
Document doc = null;
File file = new File("D:\\Loic_Workspace\\Test2\\res\\test.xml");
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
System.out.println(doc.getElementsByTagName("*").item(2).getTextContent());
System.out.println(XMLManager.getPositionByNode(doc.getElementsByTagName("*").item(2), file));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
控制台中的输出:
肉汁奶酪薯条 丹斯拉条件 2
顺便说一下,获取[#text]并不奇怪,你无法通过getTextContent()方法获取值。
希望它有所帮助;)