Java Utililty Methods XPath Get

List of utility methods to do XPath Get

Description

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

Method

ElementgetElementByXpath(Element sourceRoot, String xpathExpress)
get Element By Xpath
XPath xPath = XPATH_FACTORY.newXPath();
NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET);
if (nodes.getLength() > 0)
    return (Element) nodes.item(0);
return null;
ListgetElementsByXPath(Element parent, String name)
get Elements By X Path
List<Element> result = new ArrayList<>();
for (Node node : getNodesByXPath(parent, name)) {
    if (node instanceof Element) {
        result.add((Element) node);
return result;
StringgetElementXPath(Node elt)
Resolves the xpath of an element.
String path = "";
Node currentNode = elt;
while (!(currentNode instanceof Document)) {
    Element parent = (Element) currentNode;
    if (!parent.getTagName().equals("schema")) {
        if (!parentNodeHasMoreOfThese((Element) currentNode)) {
            path = '/' + parent.getTagName() + path;
        } else {
...
StringgetExcelPath()
get Excel Path
HashMap<String, String> configs = getConfigurations();
String path = configs.get("excel-path");
return path;
XPathFactorygetFactory()
get Factory
if (factory == null) {
    factory = createFactory();
return factory;
StringgetFastXPath(Node node)
Returns a valid FastXPath that would match the given Node in its Document.
StringBuffer buf = new StringBuffer();
Element ele;
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
    ele = (Element) node;
    break;
case Node.ATTRIBUTE_NODE:
    buf.append("/@");
...
NodegetFirstByXPath(Object obj, String xpathExpression)
Get the first node from the specified object using the specified XPath expression.
return evaluate(obj, xpathExpression, XPathConstants.NODE);
StringgetFullXPath(Node n)
get Full X Path
if (null == n)
    return null;
Node parent = null;
Stack<Node> hierarchy = new Stack<Node>();
StringBuffer buffer = new StringBuffer();
hierarchy.push(n);
switch (n.getNodeType()) {
case Node.ATTRIBUTE_NODE:
...
XPathgetHtmlXPath()
get Html X Path
XPathFactory factory = XPathFactory.newInstance();
XPath htmlPath = factory.newXPath();
htmlPath.setNamespaceContext(new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        return "http://www.w3.org/1999/xhtml";
    public String getPrefix(String namespaceURI) {
        return "h";
...
intgetInt(Object node, XPathExpression expression)
get Int
return ((Double) expression.evaluate(node, XPathConstants.NUMBER)).intValue();