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:Main.java

public static void removeChildren(Element element) {
    while (true) {
        Node child = element.getFirstChild();
        if (child == null) {
            return;
        }/* w  w  w  . j  a  v  a2  s. c  om*/
        element.removeChild(child);
    }
}

From source file:Main.java

/**
 * Removes all child nodes from the given element
 *///from w w w.j  av a  2  s. com
public static void clear(final Element element) {
    if (element == null) {
        return;
    }
    Node node;
    while ((node = element.getFirstChild()) != null) {
        element.removeChild(node);
    }
}

From source file:DOMEdit.java

public static void deleteFirstElement(Document doc) {
        Element root = doc.getDocumentElement();
        Element child = (Element) root.getFirstChild();
        root.removeChild(child);
    }// www  . j  av  a 2  s  . co m

From source file:Utils.java

/**
 * //www  .jav a 2 s.co m
 */
public static void moveContent(Element from, Element to) {
    // lets move the child nodes across
    NodeList childNodes = from.getChildNodes();
    while (childNodes.getLength() > 0) {
        Node node = childNodes.item(0);
        from.removeChild(node);
        to.appendChild(node);
    }
}

From source file:Main.java

public static void reduceChildrenList(Element elem, Class clazz) {
    for (int i = 0; i < elem.getChildNodes().getLength();) {
        if (clazz.isAssignableFrom(elem.getChildNodes().item(i).getClass())) {
            ++i;// www  . j  av a  2s . c o m
        } else {
            elem.removeChild(elem.getChildNodes().item(i));
        }
    }
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);//from   w w w.j a  va 2  s  . c om
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);
        }
    }
}

From source file:Main.java

public static void emptyXMLDocument(File file) {
    if (file.exists()) {
        Document document = loadXMLDocument(file);
        if (document != null) {
            Element root = getRoot(document);
            NodeList nodeList = root.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++)
                root.removeChild(nodeList.item(i));
            saveXMLDocument(document, file);
        }/*from   w w  w  .jav a 2 s. c  o m*/
    }
}

From source file:Main.java

public static void emptyXMLDocument(File file) {
    if (file.exists()) {
        Document document = loadXMLDocument(file);
        if (document != null) {
            Element root = getRoot(document);
            NodeList nodeList = root.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                root.removeChild(nodeList.item(i));
            }/*  ww  w. j ava 2  s .c  o m*/
            saveXMLDocument(document, file);
        }
    }
}

From source file:Main.java

/**
 * Removes all Whitespace Noted from the specified element.
 * @param e Element//from   w w  w  .jav  a2 s.com
 */
private static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);
        } else if (child instanceof Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}

From source file:Main.java

/**Get a NodeLIst of child nodes, ignoring nodes that have White space
 * @param e//www. j  ava  2s  . co  m
 * @return
 */
public static NodeList getChildNodesNoWS(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);
        }

    }
    return e.getChildNodes();
}