MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

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());
    }

}