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

/**
 * Recursively removes all text nodes containing whitespace only from a document element. Also
 * trims leading and trailing whitespace from text nodes.
 * //ww  w.  j  a  v  a 2  s . co m
 * @param e The root document element.
 */
public 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 Text)
            child.setTextContent(((Text) child).getData().trim());
        else if (child instanceof Element)
            removeWhitespaceNodes((Element) child);
    }
}

From source file:Main.java

public 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   w  ww .  j  av a 2  s . c  o m
    }
}

From source file:Main.java

public static void reduceChildrenListRecursive(Element elem) {
    for (int i = 0; i < elem.getChildNodes().getLength();) {
        Node node = elem.getChildNodes().item(i);
        if (node instanceof Element) {
            reduceChildrenListRecursive((Element) node);
            ++i;/*from   ww w .  ja v a  2s .co  m*/
        } else {
            elem.removeChild(node);
        }
    }
}

From source file:Main.java

/**
 * @see //http://www.java.net/node/667186
 */// w  w  w.ja v  a 2  s .c  o  m
public 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

public static void removeAllChilds(Element parentElem) {
    NodeList children = parentElem.getChildNodes();

    if (children != null) {
        int listLength = children.getLength();
        for (int i = listLength - 1; i >= 0; i--) {
            Element child = (Element) children.item(i);
            parentElem.removeChild(child);
        }/*w  w w  .  j a va  2  s  . c  om*/
    }
}

From source file:Main.java

/**
 * Recursively removes all text nodes from a DOM element.
 * // w  w w . ja v  a2s.co  m
 * @param element
 *        The element
 */
public static void removeTextNodes(Element element) {
    Node nextNode = element.getFirstChild();
    for (Node child = element.getFirstChild(); nextNode != null;) {
        child = nextNode;
        nextNode = child.getNextSibling();
        if (child.getNodeType() == Node.TEXT_NODE)
            element.removeChild(child);
        else if (child.getNodeType() == Node.ELEMENT_NODE)
            removeTextNodes((Element) child);
    }
}

From source file:Main.java

public static String setInnerText(Element node, String value) {
    StringBuilder sb = new StringBuilder();
    while (node.hasChildNodes()) {
        Node tn = node.getFirstChild();
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        }//from www  . j  a v a2  s  .  com
        node.removeChild(tn);
    }
    node.appendChild(node.getOwnerDocument().createTextNode(value));
    return sb.toString();
}

From source file:Main.java

/**
 * Set the text content of an element. All exisitng Text Node are
 * removed before adding a new Text Node containing the given text.
 *
 * @param element target element to set text content, cannot be null.
 * @param text content of the element, cannot be null.
 *//*  www  . j a  v  a  2  s .c om*/
public static void setElementText(Element element, String text) {

    // Remove all text element
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            element.removeChild(n);
        }
    }
    Node child = element.getFirstChild();
    Node textnode = element.getOwnerDocument().createTextNode(text);

    // insert text node as first child
    if (child == null) {
        element.appendChild(textnode);
    } else {
        element.insertBefore(textnode, child);
    }
}

From source file:Main.java

/**
 * Traverses through a DOM tree starting at the given element and removes all those text-nodes from it that only
 * contain white spaces.//from  w  ww.ja  va2  s  .c  o  m
 * 
 * @param parent
 *          the parent Element
 */
public static void removeWhitespaceTextNodes(Element parent) {
    final NodeList nl = parent.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        final Node child = nl.item(i);

        if (child.getNodeType() == Node.TEXT_NODE) {
            if (child.getNodeValue().trim().length() == 0) {
                parent.removeChild(child);
                i--; // since the child is removed counting up must be made undone
            }
        } else if (child.getNodeType() == Node.ELEMENT_NODE && child.getChildNodes().getLength() > 0) {
            removeWhitespaceTextNodes((Element) child);
        }
    }
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " begin...");
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);/*from  ww  w  . jav a 2s  .  c o m*/
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);
            logger.debug("remove child '" + nd + "' success.");
        }
    }
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " end.");
}