Example usage for javax.xml.xpath XPathFactory newInstance

List of usage examples for javax.xml.xpath XPathFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.xpath XPathFactory newInstance.

Prototype

public static XPathFactory newInstance() 

Source Link

Document

Get a new XPathFactory instance using the default object model, #DEFAULT_OBJECT_MODEL_URI , the W3C DOM.

This method is functionally equivalent to:

 newInstance(DEFAULT_OBJECT_MODEL_URI) 

Since the implementation for the W3C DOM is always available, this method will never fail.

Usage

From source file:Main.java

public static Element getElementByXPath(Element e, String xPathExpression) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    return (Element) (xPath.evaluate(xPathExpression, e, XPathConstants.NODE));

}

From source file:Main.java

public static NodeList getXmlNodeSetForExpression(String expression, String is)
        throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(is));
    return (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
}

From source file:Main.java

public static HashMap xmltoHashMap222(String xmlFile, String xpath) {
    try {/* w ww. j  a  v a 2 s . c o m*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        // XPathExpression xPathExpression =
        // xPath.compile("/history");
        File xmlDocument = new File(xmlFile);
        InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
        // String root = xPath.evaluate("/", inputSource);
        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        HashMap hashmap = new HashMap();
        for (int x = 0; x < nodes.getLength(); x++) {
            hashmap.put(nodes.item(x).getNodeName(), nodes.item(x).getTextContent());
        }
        return hashmap;
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static Vector<String> readXMLNode222(String xmlFile, String xpath) {
    try {//  w w w . j  a  va 2s  .  c  o  m
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        // XPathExpression xPathExpression =
        // xPath.compile("/history");
        File xmlDocument = new File(xmlFile);
        InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
        // String root = xPath.evaluate("/", inputSource);
        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<String> vector = new Vector<String>();
        for (int x = 0; x < nodes.getLength(); x++) {
            vector.add(nodes.item(x).getTextContent());
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

public static Vector<Element> findElements(String xpath, Node scope) throws XPathExpressionException {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();/*from   w  ww .j  ava  2 s.c  o m*/
    NodeList nl;
    Vector<Element> elements = new Vector<Element>();

    nl = (NodeList) xp.evaluate(xpath, scope, XPathConstants.NODESET);

    for (int i = 0; i < nl.getLength(); i++)
        elements.add((Element) nl.item(i));

    return elements;
}

From source file:Main.java

public static NodeList getNodeList(Document doc, String xPathAsString) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xPathAsString);
    return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
}

From source file:Main.java

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

    return xpathFactory;
}

From source file:Main.java

private static Node getNodeById(Document doc, String id) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile("//" + MESSAGE + "[@id='" + id + "']");
    return (Node) expr.evaluate(doc, XPathConstants.NODE);
}

From source file:Main.java

public static Object executeXPath(Document document, String expression, QName outputType) {
    Object result = null;//w  w  w .j  ava  2  s  . co m
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(expression);
        result = expr.evaluate(document, outputType);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

private static Object getValue(final String xpathExpression, final Node node, final QName type,
        final NamespaceContext nsContext) throws XPathExpressionException {

    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    xpath.setNamespaceContext(nsContext);
    return xpath.evaluate(xpathExpression, node, type);
}