[英]Getting Exception on evaluating an XPath expression in Java
我正在嘗試使用 Java 學習 Xpath 表達式的用法。 我正在使用 Jtidy 將 HTML 頁面轉換為 XHTML,以便我可以使用 XPath 表達式輕松解析它。 我有以下代碼:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = ConvertXHTML("https://twitter.com/?lang=fr");
//Create XPath
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath Inst= xpathfactory.newXPath();
NodeList nodes = (NodeList)Inst.evaluate("//p/@align",doc,XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); ++i)
{
Element e = (Element) nodes.item(i);
System.out.println(e);
}
public Document ConvertXHTML(String link){
try{
URL u = new URL(link);
BufferedInputStream instream=new BufferedInputStream(u.openStream());
FileOutputStream outstream=new FileOutputStream("out.xhtml");
Tidy c=new Tidy();
c.setShowWarnings(false);
c.setInputEncoding("UTF-8");
c.setOutputEncoding("UTF-8");
c.setXHTML(true);
return c.parseDOM(instream,outstream);
}
它適用於大多數 URL,但這個:
我收到此異常是因為:
javax.xml.transform.TransformerException: 索引 -1 越界.....
下面是我得到的堆棧跟蹤的一部分:
javax.xml.transform.TransformerException: Index -1 out of bounds for length 128
at java.xml/com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:366)
at java.xml/com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:303)
at java.xml/com.sun.org.apache.xpath.internal.jaxp.XPathImplUtil.eval(XPathImplUtil.java:101)
at java.xml/com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:80)
at java.xml/com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:89)
at files.ExampleCode.GetThoselinks(ExampleCode.java:50)
at files.ExampleCode.DoSomething(ExampleCode.java:113)
at files.ExampleCode.GetThoselinks(ExampleCode.java:81)
at files.ExampleCode.DoSomething(ExampleCode.java:113)
我不確定問題是否出在網站的轉換后的 xhtml 或其他方面。 誰能說出代碼中有什么問題? 任何編輯都會有所幫助。
我通常會說,來自XPath引擎深處的邊界索引異常是XPath引擎中的錯誤。 唯一的警告是XPath引擎正在搜索的DOM在結構上是否有問題; XPath處理器有權合理假設DOM是有效的,否則無效。 在這種情況下,這將是Tidy的一個錯誤,該錯誤創建了DOM。
我在 JTidy 生成的文檔上使用 xpath 評估時遇到了類似的問題。 我通過讓 JTidy 將它生成的 DOM 序列化為一個文件,然后使用 javax.xml.parsers.DocumentBuilder 解析該 xml 文件以獲得第二個版本的 DOM 來解決它。 看起來很奇怪,使用第二個避免了越界異常並起作用。 使用如下代碼:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
// If you don't do the following, it will take a full minute to parse the xml document (presumably the time-out
// period for trying to load the DTD). See https://stackoverflow.com/questions/6204827/xml-parsing-too-slow.
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = tidy.parseDOM(input, null);
FileOutputStream fos = new FileOutputStream("temp.xml");
tidy.pprint(doc, fos);
fos.close();
doc = documentBuilder.parse("temp.xml");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.