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 void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new File("Sample.xml"));
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression exp = xPath.compile(
            "/article/body/section/region[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'perfect')]");
    NodeList nl = (NodeList) exp.evaluate(doc.getFirstChild(), XPathConstants.NODESET);
    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getTextContent());
    }//  w w w  . j a v a 2  s.  c  om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    String xml1 = "<xml><product><product_id>2</product_id></product><car><product_id>12345678</product_id></car></xml>";
    String xml2 = "<xml><product><product_id>2</product_id></product><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>";

    Document doc1 = stringToDom(xml1);
    Document doc2 = stringToDom(xml2);

    XPathExpression expr1 = xpath.compile("//car/product_id/text()");
    String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING);

    XPathExpression expr2 = xpath.compile("//car/car_type/text()");
    String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING);

    System.out.println("carId: " + carId);
    System.out.println("carType: " + carType);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xmlfile = "data.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(xmlfile);

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("/root/param[@name='name2']", doc, XPathConstants.NODE);
    System.out.println(element.getTextContent());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);/*from w  ww  . j ava  2 s.c  o  m*/
    DocumentBuilder builder = docFactory.newDocumentBuilder();
    Document doc = builder.parse("data.xml");
    XPathExpression expr = XPathFactory.newInstance().newXPath().compile("/script/data");
    Object hits = expr.evaluate(doc, XPathConstants.NODESET);
    if (hits instanceof NodeList) {
        NodeList list = (NodeList) hits;
        for (int i = 0; i < list.getLength(); i++) {
            System.out.println(list.item(i).getTextContent());
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("data.xml"));

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//node3/name", document, XPathConstants.NODE);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("filename.xml"));

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    Element element = (Element) xpath.evaluate("//*[@id='one']", document, XPathConstants.NODE);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* w w w  .ja va 2s. co  m*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("test.xml");

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();
    XPathExpression expr = xpath.compile("//element[@key='property1']/text()");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<company><year id='2000'><quarter id='1' sales='80'/></year><year id='2001'><quarter id='1' sales='20'/></year></company>";
    String xpath = "/company/year[@id=2001]";
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.evaluate(xpath, new InputSource(new StringReader(xml)), XPathConstants.NODE);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(node), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml"));

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET);

    for (int idx = 0; idx < nodes.getLength(); idx++) {
        nodes.item(idx).setTextContent("new value");
    }/* w  ww.  j  a va2 s .  c  o m*/
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml")));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("path/to/file.xml"));

    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xPath = xFactory.newXPath();
    XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()");

    NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode().getNodeName().equals("Name")) {
            node.setNodeValue("new name");
        } else {/* ww  w . ja v  a  2 s .c  o  m*/
            node.setNodeValue("42");
        }
    }
}