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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11391003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:53:58  来源:igfitidea点击:

Check if xpath exist in my page

javaxpath

提问by proven?al le breton

I search a lot but never found what I want.

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

I want to control if a xpath exist in the current page.

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

I found with java/xml, php etc... But not only java.

我发现了 java/xml、php 等......但不仅是 java。

I search a simple way to check in the current page, if a xpath exist.

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

Thank you.

谢谢你。

Regards.

问候。

回答by Michele

you may use javax.xml.xpath.XPath.evalutemethod:

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

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

Example:

例子:

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!

Update

更新

How get document.
Copy-Paste of some lines of my old code:

怎么弄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();

I use a library (Tidy) to read html pages.

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

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

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

回答by Mouhammed Soueidane

You can use this utility method that would return the value of an XPath query (Be it an XML tag or an XML attribute) if it exists. Otherwise, it would throw an Exception that you would handle the way you want:

如果存在,您可以使用此实用方法返回 XPath 查询的值(无论是 XML 标记还是 XML 属性)。否则,它会抛出一个异常,您将按照自己的方式处理:

public String getValue(String xpathQuery) throws Exception
{
    Node node = null;

    try
    {

        node = (Node) xPath.evaluate(xpathQuery, doc, XPathConstants.NODE);
        if (node == null)
            throw new Exception("Xpath query "+xpathQuery+" returned no results");

        if (node.getNodeType() == Node.ATTRIBUTE_NODE)
            return node.getNodeValue();
        else
            return node.getTextContent();
    } catch (Exception e)
    {
        throw new Exception("Failed to get value from " + doc.getDocumentURI() + " using XPath expression: " + xpathQuery, e);
    }
}

回答by Michael Kay

If you are using the JAXP API, you can use an XPath expression that returns a NODE-SET and then check in the Java code whether the returned NodeList is empty; or you can specify the result type as BOOLEAN in which case you will get the boolean result directly.

如果您使用的是JAXP API,您可以使用返回一个NODE-SET 的XPath 表达式,然后在Java 代码中检查返回的NodeList 是否为空;或者您可以将结果类型指定为 BOOLEAN,在这种情况下您将直接获得布尔结果。