Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate an xpath expression in Java

Given an user input string, how can I tell whether it is a valid Xpath expression or not in Java. Just curious, since I cannot find a way to do it using javax.xml.xpath library. Thanks.

like image 992
Jeffrey.W.Dong Avatar asked Sep 30 '11 06:09

Jeffrey.W.Dong


1 Answers

I assume you want to validate the syntax but not if the expression is valid within the context of a your xml or xml schema. You can use the compile(..) method and it will throw an exception if the xpath is incorrect w.r.t the syntax.

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//book[author='Abc']/title/text()");
like image 198
Aravind Yarram Avatar answered Sep 22 '22 19:09

Aravind Yarram