Java XPath Select getXmlElements(Document inXml, String xpath)

Here you can find the source of getXmlElements(Document inXml, String xpath)

Description

get Xml Elements

License

Apache License

Declaration

public static List<Element> getXmlElements(Document inXml, String xpath) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static XPath xPath;

    public static List<Element> getXmlElements(Document inXml, String xpath) {
        try {/*from ww  w  .  ja va 2 s  . com*/
            NodeList nodeList = (NodeList) xPath.evaluate(xpath, inXml, XPathConstants.NODESET);
            List<Element> results = new ArrayList<>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                results.add((Element) nodeList.item(i));
            }
            return results;
        } catch (Exception ex) {
            throw new RuntimeException("Could not run xpath: " + xpath, ex);
        }
    }
}

Related

  1. getValueByXPath(Document document, String xpath)
  2. getValueByXPathAsInt(Document document, String xpath)
  3. getValueFromXPath(Document document, String xpathString)
  4. selectElements(Element element, String xpathExpression)
  5. selectNodeIterator(Node nContextNode, String sXPath)
  6. selectNodeList(Node contextNode, String expression)
  7. selectNodeList(Node node, String expression)