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 java.util.logging.Logger;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    private static Logger log = Logger.getLogger("XmlUtils");

    /**
     * Get node from XML document using xpath expression
     * 
     * @param doc
     * @param expression
     * @return
     * @throws XPathExpressionException
     */
    public static Node getNode(final Document doc, final String expression) throws XPathExpressionException {
        if (null == expression || expression.isEmpty() || null == doc) {
            return null;
        }
        log.finer("Executing xpath xpression " + expression);
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xPath.compile(expression);
        Object result = expr.evaluate(doc, XPathConstants.NODE);
        return (Node) result;
    }
}