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: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 www. jav  a2 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(new FileReader("input.xml"));
    Node result = (Node) xpath.evaluate("/tag1", xml, XPathConstants.NODE);
    System.out.println(result);/*from   w ww. j a va  2  s  .  co  m*/
}

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  w  w  w .jav  a2 s  .com*/
}

From source file:Main.java

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

    InputSource inputSource = new InputSource(new StringReader(XML));
    String result = (String) xPath.evaluate("//gender", inputSource, XPathConstants.STRING);
    System.out.println(result);//from   ww w.j a v  a2s. c om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document xml = db.parse("input.xml");

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    String health = (String) xpath.evaluate("/game/health", xml, XPathConstants.STRING);
    System.out.println(health);/* w  w w .j a v a2  s. com*/
}

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: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.");
}

From source file:Main.java

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

    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());
    }//  www  .ja  v a  2  s .c  o m
}

From source file:GetNameAsAttr.java

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

    Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODE);
    System.out.println(result.getValue());

    result.setValue("The Colbert Report");

}

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);
}