gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:15:31 26 4
gpt4 key购买 nike

我搜索了很多,但从未找到我想要的。

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

我找到了 java/xml、php 等...但不仅是 java。

如果 xpath 存在,我会搜索一种检查当前页面的简单方法。

谢谢。

问候。

最佳答案

你可以使用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!

更新

如何获取文档
复制粘贴我的一些旧代码行:

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();

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

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

关于java - 检查我的页面中是否存在 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11391003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com