Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import org.w3c.dom.*;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import static javax.xml.xpath.XPathConstants.NODE;

public class Main {
    /**
     * Uses the passed XPath expression to locate a single node in the passed element.
     * @param targetNode The node to search.
     * @param expression The XPath expression.
     * @return The located node or null if one is not found.
     */
    public static Node xGetNode(Node targetNode, String expression) {
        Node node = null;
        XPath xpath = null;
        try {
            xpath = XPathFactory.newInstance().newXPath();
            XPathExpression xpathExpression = xpath.compile(expression);
            node = (Node) xpathExpression.evaluate(targetNode, NODE);
            return node;
        } catch (Exception e) {
            throw new RuntimeException("XPath:Failed to locate the node:" + expression, e);
        }
    }
}