Example usage for org.w3c.dom NodeList getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("foo.xml");

    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    NodeList list = doc.getFirstChild().getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node element = list.item(i).cloneNode(true);

        if (element.hasChildNodes()) {
            Source src = new DOMSource(element);
            FileOutputStream fs = new FileOutputStream("k" + i + ".xml");
            Result dest = new StreamResult(fs);
            aTransformer.transform(src, dest);
            fs.close();/*from  w  w w.j ava  2s.com*/
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File CFile = new File("data.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);//w w w .j  a v a 2 s  .c o m
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(CFile);

    NodeList pizzas = document.getElementsByTagName("Pizza");

    for (int i = 0; i < pizzas.getLength(); i++) {
        Element pizzaSize = (Element) pizzas.item(i);
        String pSize = pizzaSize.getAttribute("Size");

        if (pSize.equalsIgnoreCase("Large"))
            System.out.println(10.0);
        if (pSize.equalsIgnoreCase("Medium"))
            System.out.println(7.0);
        if (pSize.equalsIgnoreCase("Small"))
            System.out.println(5.0);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse("data.xml");
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("staff");

    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            // get the employee element
            Element el = (Element) nl.item(i);
            String firstname = getTextValue(el, "firstname");
            String lastname = getTextValue(el, "lastname");
            String nickname = getTextValue(el, "nickname");
            int salary = getIntValue(el, "salary");

            System.out.println(firstname);
            System.out.println(lastname);
            System.out.println(nickname);
            System.out.println(salary);
        }/*from www .  j a  v  a 2 s.c  om*/
    }
}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    File fXmlFile = new File("data.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();
    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    NodeList nList = doc.getElementsByTagName("staff");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("Staff id : " + eElement.getAttribute("id"));
            System.out.println(//from w  ww . j  av a2  s  .c  o  m
                    "First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
            System.out.println(
                    "Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
            System.out.println(
                    "Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
        }
    }
}

From source file:MainClass.java

public static void main(String[] args)
        throws IOException, ParserConfigurationException, org.xml.sax.SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);/*  ww  w . j a  v a 2  s. co m*/
    factory.setCoalescing(true); // Convert CDATA to Text nodes
    factory.setNamespaceAware(false); // No namespaces: this is default
    factory.setValidating(false); // Don't validate DTD: also default

    DocumentBuilder parser = factory.newDocumentBuilder();

    Document document = parser.parse(new File(args[0]));

    NodeList sections = document.getElementsByTagName("sect1");
    int numSections = sections.getLength();
    for (int i = 0; i < numSections; i++) {
        Element section = (Element) sections.item(i); // A <sect1>

        Node title = section.getFirstChild();
        while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
            title = title.getNextSibling();

        if (title != null)
            System.out.println(title.getFirstChild().getNodeValue());
    }
}

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.j  av  a  2 s.c  om

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(
            new StringReader("<emp><empname><firstName></firstName><lastName></lastName></empname></emp>")));
    NodeList customerNodes = doc.getElementsByTagName("empname");
    for (int i = 0; i < customerNodes.getLength(); i++) {
        NodeList children = customerNodes.item(i).getChildNodes();
        for (int j = 0; j < children.getLength(); j++) {
            String childNode = children.item(j).getNodeName();
            if (childNode.equalsIgnoreCase("firstName")) {
                children.item(j).setTextContent(String.valueOf("John"));
            } else if (childNode.equalsIgnoreCase("lastName")) {
                children.item(j).setTextContent(String.valueOf("Doe"));
            }//from ww w.j ava 2s .  co  m
        }
    }
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    System.out.println(writer.getBuffer().toString());
}

From source file:Main.java

public static void main(String arg[]) throws Exception {
    String xmlRecords = "<data><employee><name>A</name>" + "<title>Manager</title></employee></data>";

    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);/*  w  ww. j av  a 2s  . c  om*/
    NodeList nodes = doc.getElementsByTagName("employee");

    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);

        NodeList name = element.getElementsByTagName("name");
        Element line = (Element) name.item(0);
        System.out.println("Name: " + getCharacterDataFromElement(line));

        NodeList title = element.getElementsByTagName("title");
        line = (Element) title.item(0);
        System.out.println("Title: " + getCharacterDataFromElement(line));
    }

}

From source file:MainClass.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  www  . j  a  v  a2s .  c  om
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse("yourFile.xml");
    Element rootElement = doc.getDocumentElement();
    NodeList children = rootElement.getChildNodes();
    Node current = null;
    int count = children.getLength();
    for (int i = 0; i < count; i++) {
        current = children.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) current;
            if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
                element.setAttribute("showPageNumbers", "no");
            }
        }
    }

    System.out.println(doc.getDocumentElement());
}

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());
    }/*from w  ww .  j av a 2s .  c  o m*/
}