Check if xpath exist in my page

12,516

Solution 1

you may use javax.xml.xpath.XPath.evalute method:

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)

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:

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.

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

Solution 2

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:

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

Solution 3

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.

Share:
12,516
provençal le breton
Author by

provençal le breton

Updated on June 24, 2022

Comments

  • provençal le breton
    provençal le breton about 2 years

    I search a lot but never found what I want.

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

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

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

    Thank you.

    Regards.