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

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

    String result = xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")));
    System.out.println(result);/*from www. j av a  2  s  . c  om*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("file:////tmp/whatever.xml");
    String version = xpath.evaluate("//behavior/@version", doc);
    System.out.println(version);//from www  .  j  a  va 2 s. c  o m
}

From source file:Main.java

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

    InputSource xml = new InputSource("input.xml");
    String health = (String) xpath.evaluate("/Game/health", xml, XPathConstants.STRING);
    System.out.println(health);/*from w ww  . j  a  v a  2  s . co m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputSource inputSource = new InputSource("input.xml");
    XPath xPath = XPathFactory.newInstance().newXPath();
    String text = xPath.evaluate("/note/body", inputSource);
    System.out.println(text);//  w ww  .  j  av  a2s .com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("/bookshelf/shelf/book/*",
            new InputSource(Main.class.getResourceAsStream("/books.xml")), XPathConstants.NODESET);
    System.out.println("First  node: " + nodes.item(0));
    System.out.println("Second node: " + nodes.item(1));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xp = XPathFactory.newInstance().newXPath();
    InputSource xml = new InputSource(new FileInputStream("input.xml"));
    double count = (Double) xp.evaluate("count(//slot[@name='slot1']/port)", xml, XPathConstants.NUMBER);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    String xpathExpression = "/howto/topic/@name";
    InputSource inputSource = new InputSource("howto.xml");

    NodeList nodes = (NodeList) xpath.evaluate(xpathExpression, inputSource, XPathConstants.NODESET);

    int j = nodes.getLength();

    for (int i = 0; i < j; i++) {
        System.out.println(nodes.item(i).getTextContent());
    }//from  w ww .  ja va  2 s .  co  m

}

From source file:GetSeriesId.java

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

    Double result = (Double) xPath.evaluate("/schedule/@seriesId", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println(result.intValue());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();// ww w.j  av a2 s  . co  m

    InputSource xml = new InputSource("input.xml");
    NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", xml, XPathConstants.NODESET);

    for (int x = 0; x < leafNodeObjects.getLength(); x++) {
        System.out.print("nodeElement = ");
        System.out.print(leafNodeObjects.item(x).getNodeName());
        System.out.print(" and node value = ");
        System.out.println(leafNodeObjects.item(x).getTextContent());
    }
}

From source file:GuestListCounter.java

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

    Number shows = (Number) xPath.evaluate("count(/schedule/show)", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NUMBER);
    System.out.println("Document has " + shows.intValue() + " shows.");
}