Example usage for javax.xml.xpath XPathFactory newXPath

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

Introduction

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

Prototype

public abstract XPath newXPath();

Source Link

Document

Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document retval = dbf.newDocumentBuilder().newDocument();
    Element parent = retval.createElement("parent");
    retval.appendChild(parent);//from  w  w  w  .  j  a va 2 s.com

    Element child1 = retval.createElement("child");
    child1.setTextContent("child.text");
    parent.appendChild(child1);
    Element child2 = retval.createElement("child");
    child2.setTextContent("child.text.2");
    parent.appendChild(child2);

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    System.out.println(xPath.evaluate("//child/text()", retval, XPathConstants.NODE).getClass());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);// ww  w  .  j  a  v  a 2 s .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 {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml"))));

    String expression = "//Home/data";
    XPathExpression xPathExpression = xPath.compile(expression);

    NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
    xPathExpression = xPath.compile("@type");

    for (int i = 0; i < elem1List.getLength(); i++) {
        System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING));
    }/* w  ww . java2s.c o  m*/
}

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 {//  w  ww . j a v a 2  s .co  m
            node.setNodeValue("42");
        }
    }
}

From source file:Main.java

public static void main(String[] args)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*from w w w . j av  a2  s .  co m*/
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("books.xml");

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//numDocs");

    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 {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false); // never forget this!
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse("/data.xml");

    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xpath = xPathFactory.newXPath();

    XPathExpression xPathExpression = xpath.compile("//city/text()");

    Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);

    System.out.println(result.toString());

    NodeList nodes = (NodeList) result;

    System.out.println(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }//  ww  w  .  j av a 2 s  .c o m
}

From source file:Main.java

public static void main(String[] args) throws Throwable {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    String entryType = null;// ww w  .j a  v a 2s  . co  m
    XPathExpression[] expressions = new XPathExpression[] {
            xpath.compile("local-name(*[local-name() = 'package'])"),
            xpath.compile("local-name(*[local-name() = 'libapp'])"),
            xpath.compile("local-name(*[local-name() = 'module'])") };

    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = fac.newDocumentBuilder();
    Document doc = parser.parse(args[0]);

    for (int i = 0; i < expressions.length; i++) {
        String found = (String) expressions[i].evaluate(doc.getDocumentElement(), XPathConstants.STRING);
        entryType = mappings.get(found);
        if (entryType != null && !entryType.trim().isEmpty()) {
            break;
        }
    }
    System.out.println(entryType);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> names = new ArrayList<>();
    URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265");
    InputStream is = oracle.openStream();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);// w  w w  . j  av a 2s  . com
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(is);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//*:*/@*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nl = (NodeList) result;
    for (int i = 0; i < nl.getLength(); i++) {
        names.add(nl.item(i).getNodeName());
        Node node = nl.item(i);
        String path = "." + node.getNodeName() + " = " + node.getNodeValue();
        node = ((Attr) node).getOwnerElement();
        while (node != null) {
            path = node.getNodeName() + '/' + path;
            node = node.getParentNode();
        }
        System.out.println(path);
    }
}

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:MainClass.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat xmlDateFormat = new SimpleDateFormat("MM.dd.yy");

    MapVariableResolver resolver = new MapVariableResolver();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document document = builder.parse(new File("t.xml"));

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    xPath.setXPathVariableResolver(resolver);
    XPathExpression expression = xPath.compile("/schedule/show[@date=$date]/guest");

    String formattedDate = xmlDateFormat.format(new Date(2006, 5, 14));
    resolver.addVariable(null, "date", formattedDate);
    Element guest = (Element) expression.evaluate(document, XPathConstants.NODE);

    System.out.println(guest.getElementsByTagName("name").item(0).getTextContent());
}