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.
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 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.
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.
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
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With