Java Utililty Methods XPath Validate

List of utility methods to do XPath Validate

Description

The list of methods to do XPath Validate are organized into topic(s).

Method

StringvalidateXPath(final Document doc, final String xpathExpression)
Validates the XPath expression entered by the user in a wizard.
try {
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
    if (nodes.getLength() == 1) {
        if (nodes.item(0).getNodeType() == Node.ATTRIBUTE_NODE) {
            return "attribute"; 
        } else if (nodes.item(0).getNodeType() == Node.ELEMENT_NODE) {
            return "single"; 
...
voidvalidateXPath(String path)
validate X Path
final XPath xPath = XPathFactory.newInstance().newXPath();
try {
    xPath.compile(path);
} catch (XPathExpressionException e) {
    throw new IllegalArgumentException("Argument is not valid XPath string", e);
StringvalidateXPath(String xml, String... tests)
A helper method which validates a String against an array of XPath test strings.
if (tests == null || tests.length == 0)
    return null;
Document document = null;
try {
    document = getXmlDocumentBuilder()
            .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
} catch (UnsupportedEncodingException e1) {
    throw new RuntimeException("Totally weird UTF-8 exception", e1);
...
StringvalidateXPathExpression(String xpathExpression)
Validates a XPath expression.
String msg = null;
try {
    X_PATH.compile(xpathExpression);
} catch (Exception e) {
    String cause = null;
    if (e.getCause() != null)
        e.getCause().getMessage();
    else
...