Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:Main.java

public static Node getNoge(String path, Node node) throws RuntimeException {
    path = path.trim();//  w  ww . j  a v  a 2  s  .c  om
    try {
        XPath xp = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xp.compile(path);

        Node lst = (Node) expr.evaluate(node, XPathConstants.NODE);
        return lst;

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

From source file:Main.java

public static void updateNodeToXml(String nodeXpathStr, String xmlFilePath, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;/* w w w .  ja va2  s.  c o  m*/
    if (xmlFilePath == null || nodeXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeXpathStr!");
    ;
    pNode.setTextContent(value);

    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            ((Element) pNode).setAttribute(key, attr.get(key));
    }

    writeToXmlFile(doc, xmlFilePath);
}

From source file:Main.java

public static Node getSingleNode(String fileName, String xpathStr)
        throws SAXException, IOException, XPathExpressionException {
    Node result = null;/*from w ww.  ja  va 2s  .  co m*/
    Document doc = null;
    doc = dombuilder.parse(fileName);
    //"//db/@dbType"
    result = (Node) xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODE);
    return result;
}

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.  j  a  va2 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 void addNodeToXml(String nodeParentXpathStr, String xmlFilePath, String nodeName, String value,
        Map<String, String> attr) throws Exception {
    Document doc = null;/*from  w  w  w.j ava2s. c  o m*/
    if (xmlFilePath == null || nodeParentXpathStr == null || nodeName == null || nodeParentXpathStr == null)
        throw new Exception("some parameters can not be null!");
    doc = dombuilder.parse(new File(xmlFilePath));
    Node pNode = (Node) xpath.compile(nodeParentXpathStr).evaluate(doc, XPathConstants.NODE);
    if (pNode == null)
        throw new Exception("can not find the node specified in nodeParentXpathStr!");
    ;

    Element newNode = doc.createElement(nodeName);
    newNode.setTextContent(value);
    if (attr != null && !attr.isEmpty()) {
        for (String key : attr.keySet())
            newNode.setAttribute(key, attr.get(key));
    }

    pNode.appendChild(newNode);
    writeToXmlFile(doc, xmlFilePath);

}

From source file:Main.java

public static Node selectSingleNode(final Node sourceNode, final String xPathExpression) {
    Node result;//from   ww w. ja v a 2 s.c om
    XPathFactory factory = XPathFactory.newInstance(); // http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
    XPath xPathParser = factory.newXPath();

    try {
        result = (Node) xPathParser.evaluate(xPathExpression, sourceNode, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        result = null;
    }

    return result;
}

From source file:Main.java

private static Node evaluate(Document doc) throws XPathExpressionException {
    return (Node) expression.evaluate(doc, XPathConstants.NODE);
}

From source file:Main.java

/**
 * Get a node from an XPath expression./*  ww  w.  ja v  a  2  s . c  o m*/
 * 
 * @param node node
 * @param expr XPath expression
 * @return node list
 * @throws XPathExpressionException 
 */
public static Node getNode(Node node, XPathExpression expr) throws XPathExpressionException {
    return (Node) expr.evaluate(node, XPathConstants.NODE);
}

From source file:Main.java

public static Node selectSingleNode(String express, Object source) {
    Node result = null;/* w ww.  java2s. c o m*/
    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    try {
        result = (Node) xpath.evaluate(express, source, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

/**
 * Checks for a given element whether it can find an attribute which matches
 * the XPath expression supplied. Returns {@link Node} if exists.
 * /*from   w  w  w .ja  v  a  2  s .co  m*/
 * @param xPathExpression the xPathExpression (required)
 * @param element (required)
 * @return the Node if discovered (null if not found)
 */
public static Node findFirstAttribute(final String xPathExpression, final Element element) {
    Node attr = null;
    try {
        XPathExpression expr = COMPILED_EXPRESSION_CACHE.get(xPathExpression);
        if (expr == null) {
            expr = XPATH.compile(xPathExpression);
            COMPILED_EXPRESSION_CACHE.put(xPathExpression, expr);
        }
        attr = (Node) expr.evaluate(element, XPathConstants.NODE);
    } catch (final XPathExpressionException e) {
        throw new IllegalArgumentException("Unable evaluate xpath expression", e);
    }
    return attr;
}