检查我的页面中是否存在xpath

pro*_*ton 5 java xpath

我搜索了很多但从未找到我想要的东西.

我想控制当前页面中是否存在xpath.

我发现用java/xml,php等...但不仅仅是java.

如果存在xpath,我会以一种简单的方式检查当前页面.

谢谢.

问候.

Mic*_*ele 12

你可以使用javax.xml.xpath.XPath.evalute方法:

http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPath.html#evaluate(java.lang.String,%20java.lang.Object,%20javax.xml. namespace.QName)

例:

XPathFactory factory = XPathFactory.newInstance();
XPath path = factory.newXPath();
Node node = (Node) path.evaluate("//myXPath", document, XPathConstants.NODE);
if (node == null)
    // don't exists
else
    // I exist!
Run Code Online (Sandbox Code Playgroud)

更新

怎么弄document.
复制粘贴我的旧代码的一些行:

BufferedInputStream bufferPage = new BufferedInputStream(new URL("http://www.yourUrl.com").openStream());

Tidy tidy = new Tidy();
tidy.setQuiet(true);
tidy.setShowWarnings(false);
tidy.setInputEncoding("UTF-8");
Document document = tidy.parseDOM(bufferPage, null);
document.normalize();
Run Code Online (Sandbox Code Playgroud)

我使用库(Tidy)来读取html页面.

http://jtidy.sourceforge.net/download.html
http://jtidy.sourceforge.net/apidocs/index.html?org/w3c/tidy/package-tree.html