Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.xpath.*;
import org.w3c.dom.Node;

public class Main {
    private static XPathFactory xpathFactory = null;
    private static XPath xpath = null;

    /**
     * This method uses XPath to get you the String from the provided node.
     * @param parentNode The Node to start at.
     * @param expression The XPath Expression to evaluate.
     * @return The String that you want to extract using the XPath expression.
     * @throws XPathExpressionException
     *
     * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
     */
    public static String getString(Node parentNode, String expression) throws XPathExpressionException {
        return (String) getXPath().evaluate(expression, parentNode, XPathConstants.STRING);
    }

    /**
     * This method gives you an XPath object from the XPath Factory.
     * @return A new XPath object.
     *
     * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
     */
    protected static synchronized XPath getXPath() {
        if (xpath == null) {
            xpath = getXPathFactory().newXPath();
        }

        return xpath;
    }

    /**
     * This method yields the XPath Factory (creating it if necessary).
     * @return The XPathFactory object.
     *
     * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
     */
    protected static synchronized XPathFactory getXPathFactory() {
        if (xpathFactory == null) {
            xpathFactory = XPathFactory.newInstance();
        }

        return xpathFactory;
    }
}