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 NodeList getNogeList(String path, Node node) throws RuntimeException {
    path = path.trim();//  w w  w.j av a2 s .  c o m
    try {
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);

        NodeList lst = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
        return lst;

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

static public ArrayList<Element> selectElements(Element element, String xpathExpression) throws Exception {
    ArrayList<Element> resultVector = new ArrayList<Element>();
    if (element == null) {
        return resultVector;
    }/*from  w  ww.  j  a  va2  s  .  co  m*/
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                resultVector.add((Element) node);
            }
        }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, element, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            resultVector.add((Element) node);
        }
    }
    return resultVector;
}

From source file:Main.java

public static XPath buildXPath(NamespaceContext namespaceContext) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(namespaceContext);
    return xPath;
}

From source file:Main.java

public static String extractValue(String xml, String xpathExpression) {
    String actual;/*  ww  w  .ja  v a  2  s.c o m*/
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        byte[] bytes = xml.getBytes("UTF-8");
        InputStream inputStream = new ByteArrayInputStream(bytes);
        Document doc = docBuilder.parse(inputStream);
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();

        actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return actual;
}

From source file:Main.java

public static Vector<HashMap> xmlToVector222(InputStream is, String xpath) {
    try {/*from w  ww .  j  a va  2  s.c o  m*/
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        InputSource inputSource = new InputSource(is);

        NodeList nodes = (NodeList) xPath.evaluate(xpath, inputSource, XPathConstants.NODESET);
        Vector<HashMap> vector = new Vector<HashMap>();

        for (int x = 0; x < nodes.getLength(); x++) {
            NodeList nodeList = nodes.item(x).getChildNodes();
            HashMap hashmap = new HashMap();
            for (int y = 0; y < nodeList.getLength(); y++) {
                Node node = nodeList.item(y);
                if (!node.getNodeName().equals("#text")) {
                    hashmap.put(node.getNodeName(), node.getTextContent());
                }
            }
            vector.add(hashmap);
        }
        return vector;
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Vector();
    }
}

From source file:Main.java

public static NodeList getComponentsWithAttribute(Node xmlDocument, String attribute)
        throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = null;/*from   w  w w . java  2  s. c o  m*/
    NodeList n1 = null;
    expr = xpath.compile("//*[@" + attribute + "]");
    n1 = (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    return n1;

}

From source file:Main.java

public static List<Node> selectMultipleNodes(String expression, Node node, NamespaceContext namespaceContext)
        throws XPathException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(namespaceContext);
    NodeList nodeList = (NodeList) xpath.evaluate(expression, node, XPathConstants.NODESET);
    List<Node> nodes = new ArrayList<Node>();
    int nodeCount = nodeList.getLength();
    for (int i = 0; i < nodeCount; i++) {
        nodes.add(nodeList.item(i));//from   www  .j  a  v a 2s  . c o  m
    }
    return nodes;
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression) throws Exception {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }//  w  w  w . ja va 2  s. c o m
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

public static Object execXPath(org.w3c.dom.Node node, String pattern, QName xPathConstantsType)
        throws Exception {
    return XPathFactory.newInstance().newXPath().compile(pattern).evaluate(node, xPathConstantsType);
}

From source file:Main.java

public static Node selectSingleNode(String express, Object source) {
    Node result = null;//from   w ww. ja  v a2s  .com
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}