Java Utililty Methods XPath Expression

List of utility methods to do XPath Expression

Description

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

Method

PropertiesloadFromXmlNode(String xmlFile, String prefix)
load From Xml Node
XPath xpath = XPathFactory.newInstance().newXPath();
InputSource source = new InputSource(xmlFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(source);
XPathExpression expr = xpath.compile(prefix + "/*");
NodeList list = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Properties props = new Properties();
...
NodeListloadNodeList(String file, String path)
load Node List
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(file);
    return getDocNodeList(doc, path);
} catch (ParserConfigurationException e) {
    e.printStackTrace();
...
Documentmerge(XPathExpression expression, InputStream... files)
merge
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document base = docBuilder.parse(files[0]);
Node results = (Node) expression.evaluate(base, XPathConstants.NODE);
if (results == null) {
    throw new IOException(files[0] + ": expression does not evaluate to node");
for (int i = 1; i < files.length; i++) {
    Document merge = docBuilder.parse(files[i]);
    Node nextResults = (Node) expression.evaluate(merge, XPathConstants.NODE);
    while (nextResults.hasChildNodes()) {
        Node kid = nextResults.getFirstChild();
        nextResults.removeChild(kid);
        kid = base.importNode(kid, true);
        results.appendChild(kid);
return base;
XPathExpressionmodelPathExpr()
Constructs XPath for disecting model path
XPathExpression pathExpr = fact.newXPath().compile("//arr[@name='model_path']/str");
return pathExpr;
StringparseXMLValue(String xml, String xpathExpression)
parse XML Value
try (InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(is);
    xpath.setNamespaceContext(ihcNamespaceContext);
    return (String) xpath.evaluate(xpathExpression, inputSource, XPathConstants.STRING);
XPathExpressionparseXPath(String expression, NamespaceContext nsContext)
parse X Path
XPath xpath = XPATH_FACTORY.newXPath();
xpath.setNamespaceContext(nsContext);
return xpath.compile(expression);
booleanpathExists(XPath xpath, String expression, Object object)
Convenience method that performs an xpath evaluation to determine whether the expression evaluates to true (a node exists).
return ((Boolean) xpath.evaluate(expression, object, XPathConstants.BOOLEAN)).booleanValue();
StringreadXmlNodeChildFromFile(String xPath, String path, NamespaceContext nsContext)
read Xml Node Child From File
synchronized (path.intern()) {
    File f = new File(path);
    DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
    xmlFact.setNamespaceAware(nsContext != null);
    try {
        DocumentBuilder builder = xmlFact.newDocumentBuilder();
        Document doc = builder.parse(f);
        XPath xpath = XPathFactory.newInstance().newXPath();
...
voidreplaceXPathNode(Node replaceThis, Node replaceWith)
replace X Path Node
Node parentNode = replaceThis.getParentNode();
parentNode.insertBefore(replaceWith, replaceThis);
parentNode.removeChild(replaceThis);
NodesingleXPathNode(String expression, Node node)
single X Path Node
XPath xp = XPathFactory.newInstance().newXPath();
return (Node) xp.evaluate(expression, node, XPathConstants.NODE);