Element: NodeList getChildNodes() : Element « org.w3c.dom « Java by API





Element: NodeList getChildNodes()


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MainClass {

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    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());
  }

}

 










Related examples in the same category

1.Element: getElementsByTagName(String name)
2.Element: String getTagName()
3.Element: setAttribute(String name, String value)