Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if xpath exist in my page

Tags:

java

xpath

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.

like image 793
provençal le breton Avatar asked Jul 09 '12 07:07

provençal le breton


People also ask

How do I check if XPath exists?

Use the fn:exists XPath function to test whether the value of an input element is present. Note: An XML element that has the xsi:nil attribute set is considered to be present.

How to check XML node exists or not in Java?

How to check if xml node exists? To verify if node or tag exists in XML content, you can execute an xpath expression against DOM document for that XML and count the matching nodes. matching nodes > zero – XML tag / attribute exists. matching nodes <= zero – XML tag / attribute does not exist.

Can I use XPath in JavaScript?

This document describes the interface for using XPath in JavaScript internally, in extensions, and from websites. Mozilla implements a fair amount of the DOM 3 XPath, which means that XPath expressions can be run against both HTML and XML documents.


3 Answers

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

like image 125
2 revs Avatar answered Sep 24 '22 22:09

2 revs


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);
    }
}
like image 25
Mouhammed Soueidane Avatar answered Sep 24 '22 22:09

Mouhammed Soueidane


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.

like image 30
Michael Kay Avatar answered Sep 21 '22 22:09

Michael Kay