Example usage for javax.xml.xpath XPathConstants NODESET

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

Introduction

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

Prototype

QName NODESET

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

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Maps to Java org.w3c.dom.NodeList .

Usage

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 {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xp = xpf.newXPath();/*  w ww .j a  v a2s  . c  o 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: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());
    }//w w w. java2 s. c o m

}

From source file:Main.java

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

    FileReader reader = new FileReader("input.xml");
    InputSource xml = new InputSource(reader);
    NodeList titleNodes = (NodeList) xPath.evaluate("//item/title", xml, XPathConstants.NODESET);

    for (int x = 0; x < titleNodes.getLength(); x++) {
        System.out.println(titleNodes.item(x).getTextContent());
    }/*w  w w  . j av  a  2 s  . c o  m*/
}

From source file:Main.java

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

    NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODESET);
    for (int i = 0; i < shows.getLength(); i++) {
        Element show = (Element) shows.item(i);
        String guestName = xPath.evaluate("guest/name", show);
        System.out.println(guestName);
        System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date"));
    }//  w w w .  ja  v a  2  s.co m

}

From source file:GuestList.java

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

    NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader("tds.xml")),
            XPathConstants.NODESET);
    for (int i = 0; i < shows.getLength(); i++) {
        Element show = (Element) shows.item(i);
        String guestName = xPath.evaluate("guest/name", show);
        String guestCredit = xPath.evaluate("guest/credit", show);

        System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - " + guestName
                + " (" + guestCredit + ")");
    }/*from w  ww.jav  a2  s. c  om*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("Test.xml"));
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression exp = xPath.compile("//data");
    NodeList nl = (NodeList) exp.evaluate(doc, XPathConstants.NODESET);
    System.out.println("Found " + nl.getLength() + " results");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);/*w  ww . ja  v  a2 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 {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xPath = xpf.newXPath();
    XPathExpression schoolNameExpression = xPath.compile("SchoolName");
    XPathExpression classNameExpression = xPath.compile("Classes/Class/ClassName");

    InputSource inputSource = new InputSource("input.xml");
    NodeList schoolNodes = (NodeList) xPath.evaluate("/Data/Schools/School", inputSource,
            XPathConstants.NODESET);
    for (int x = 0; x < schoolNodes.getLength(); x++) {
        Node schoolElement = schoolNodes.item(x);
        System.out.println(schoolNameExpression.evaluate(schoolElement, XPathConstants.STRING));
        NodeList classNames = (NodeList) classNameExpression.evaluate(schoolElement, XPathConstants.NODESET);
        for (int y = 0; y < classNames.getLength(); y++) {
            System.out.println(classNames.item(y).getTextContent());
        }/*w  w  w  . j av a  2s  .co  m*/
    }
}

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  w w  .j  a  va 2 s .  co  m
}