Example usage for org.w3c.dom NodeList item

List of usage examples for org.w3c.dom NodeList item

Introduction

In this page you can find the example usage for org.w3c.dom NodeList item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the collection.

Usage

From source file:Main.java

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

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*  w  w  w  .  j  a va  2 s.  c  o 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 {
    InputStream is = new URL("http://www.your server.com/daily.xml").openStream();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(is);/* ww  w . j  a v  a 2 s  .c o m*/
    NodeList nodeList = doc.getElementsByTagName("v");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getAttribute("currency").equals("BRL")) {
                System.out.println(element.getAttribute("rate"));
            }
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  w  w w . jav a  2 s  .  c  o  m

    factory.setExpandEntityReferences(false);

    Document doc1 = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc1.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml"));

    // Make a copy of the element subtree suitable for inserting into doc2
    Node node = doc2.importNode(element, true);

    // Get the parent
    Node parent = node.getParentNode();

    // Get children
    NodeList children = node.getChildNodes();

    // Get first child; null if no children
    Node child = node.getFirstChild();

    // Get last child; null if no children
    child = node.getLastChild();

    // Get next sibling; null if node is last child
    Node sibling = node.getNextSibling();

    // Get previous sibling; null if node is first child
    sibling = node.getPreviousSibling();

    // Get first sibling
    sibling = node.getParentNode().getFirstChild();

    // Get last sibling
    sibling = node.getParentNode().getLastChild();

}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(Main.class.getResourceAsStream("/foo.xml"));
    NodeList nodeNodeList = document.getElementsByTagName("node");
    for (int i = 0; i < nodeNodeList.getLength(); i++) {
        Node nNode = nodeNodeList.item(i);
        System.out.println(nNode.getAttributes().getNamedItem("lat").getNodeValue());
        System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());
    }//from   w w w.j  ava2s .  co m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//ww w  .  ja v a2s  . c o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        element = (Element) list.item(i);
        pi = doc.createProcessingInstruction("target", "instruction=" + i);
        element.appendChild(pi);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File("input.xml"));
    NodeList nodeList = document.getElementsByTagName("Item");
    for (int x = 0, size = nodeList.getLength(); x < size; x++) {
        System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());
    }/*from w ww  .j av  a 2  s  . c  o m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//w ww.  jav  a 2s. c  o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);

        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:Main.java

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

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document dom = parser.getDocument();

    NodeList games = dom.getElementsByTagName("game");

    for (int i = 0; i < games.getLength(); i++) {
        Node aNode = games.item(i);
        System.out.println(aNode.getFirstChild().getNodeValue());

        NamedNodeMap attributes = aNode.getAttributes();

        for (int a = 0; a < attributes.getLength(); a++) {
            Node theAttribute = attributes.item(a);
            System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
        }//ww w.  j  a  va  2 s  .  co  m
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//  ww  w .  ja v  a 2  s.c  o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    // Find all elements with the name "entry" and append a comment
    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);
        // Add the comment after this element
        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    File stocks = new File("Stocks.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(stocks);
    doc.getDocumentElement().normalize();

    System.out.println(doc.getDocumentElement().getNodeName());
    NodeList nodes = doc.getElementsByTagName("stock");

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            System.out.println("Stock Symbol: " + getValue("symbol", element));
            System.out.println("Stock Price: " + getValue("price", element));
            System.out.println("Stock Quantity: " + getValue("quantity", element));
        }//from   w ww .ja va 2  s  .c om
    }
}