我有jdom2 XPath的問題:與jdom2 XPath查詢結果不明
test.xhtml代碼:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
<head>
<title>mypage</title>
</head>
<body>
<div class="in">
<a class="nextpage" href="url.html">
<img src="img/url.gif" alt="to url.html" />
</a>
</div>
</body>
</html>
Java代碼:
Document document;
SAXBuilder saxBuilder = new SAXBuilder();
document = saxBuilder.build("test2.html");
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp = xpfac.compile("//a[@class = 'nextpage']", Filters.element());
for (Element att : xp.evaluate(document)) {
System.out.println("We have target " + att.getAttributeValue("href"));
}
但只是這個我不能得到任何元素。我發現當查詢是//*[@class = 'nextpage']
時,它發現它。
We have target url.html
它必須是一些與命名空間或以其他任何標題,因爲沒有它,它可以產生一些輸出。我不知道我做錯了什麼。
「它必須是具有名稱空間的東西」 - 正確。我已經鏈接到的「可能重複」問題是谷歌給我的第一個命令「jdom xpath namespace」 –
現在似乎已經解決 - changes:Namespace namespace = Namespace.getNamespace(「my」,「http://www.w3 .ORG/1999/XHTML「);和XPathExpression xp = xpfac.compile(「// my:a [@class ='nextpage']」,Filters.element(),null,namespace); –
d3im
這個問題似乎是脫離主題,因爲它現在已經_solved_(見OP的評論)。 – devnull