Example usage for org.w3c.dom Element removeChild

List of usage examples for org.w3c.dom Element removeChild

Introduction

In this page you can find the example usage for org.w3c.dom Element removeChild.

Prototype

public Node removeChild(Node oldChild) throws DOMException;

Source Link

Document

Removes the child node indicated by oldChild from the list of children, and returns it.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from w ww .  j  av  a2 s .  co  m*/
    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());
}

From source file:Main.java

public static void removeChilds(Element e) {
    while (e.hasChildNodes())
        e.removeChild(e.getFirstChild());
}

From source file:Main.java

License:asdf

public static void deleteFirstElement(Document doc) {
    Element root = doc.getDocumentElement();
    Element child = (Element) root.getFirstChild();
    root.removeChild(child);
}

From source file:Main.java

public static void removeAll(Element node, String subElement) {
    for (Element e : findAll(node, subElement))
        node.removeChild(e);
}

From source file:Main.java

/**
 * Delete child element by name//from  w  ww  . j  a  va  2s.c om
 * @param element parent element
 * @param childName child name
 */
public static void removeChild(Element element, String childName) {
    Element childElement = findChildElement(element, childName);
    if (childElement != null) {
        childElement.removeChild(element);
    }
}

From source file:Main.java

/**
 * Remove all child nodes of the given node.
 *
 * @param aElement// w w  w  .ja v  a 2 s.  com
 *        The element whose children are to be removed.
 */
public static void removeAllChildElements(@Nonnull final Element aElement) {
    while (aElement.getChildNodes().getLength() > 0)
        aElement.removeChild(aElement.getChildNodes().item(0));
}

From source file:Main.java

public static void removeContent(Element element) {
    NodeList nodeList = element.getChildNodes();
    while (nodeList.getLength() > 0) {
        element.removeChild(nodeList.item(0));
    }//from   w ww.ja  v a 2s.  c o m

}

From source file:Main.java

public static void deleteFirstElement(Document doc) {
    Element root = doc.getDocumentElement();
    if (root.getFirstChild() instanceof Element) {
        Element child = (Element) root.getFirstChild();
        root.removeChild(child);

    }/*w w w  .j a  va 2s  .co m*/
}

From source file:Utils.java

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
        String value) {/*  ww w.  ja v a  2  s  .c o m*/
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }
    Element ret = (Element) parent.getElementsByTagName(child).item(0);
    if (ret.getFirstChild() != null) {
        ret.removeChild(ret.getFirstChild());
    }
    ret.appendChild(document.createTextNode(value));
    return ret;
}

From source file:Main.java

public static void removeAllChildren(Element deps) {
    NodeList childNodes = deps.getChildNodes();

    for (int i = childNodes.getLength() - 1; i >= 0; i--) {
        Node item = childNodes.item(i);
        deps.removeChild(item);
    }//from www .j  a va 2  s  .  co m
}