Element: String getTagName() : Element « org.w3c.dom « Java by API






Element: String getTagName()



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;
    for (int i = 0; i < children.getLength(); i++) {
      current = children.item(i);
      if (current.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) current;
        if (element.getTagName().equalsIgnoreCase("tableOfContents")) {
          rootElement.removeChild(element);
        }
      }
    }

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

}

 








Related examples in the same category

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