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

private static void removeWhitespaces(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength();) {
        Node n = children.item(i);
        if (n instanceof Element) {
            removeWhitespaces((Element) n);
            ++i;/*from w  w w . j  a  va 2 s . co m*/
        } else if (n instanceof Text && ((Text) n).getTextContent().trim().length() == 0) {
            element.removeChild(n);
        } else {
            ++i;
        }
    }
}

From source file:Main.java

/**
 * Delete children elements for Element by element name
 *//*from   w  w  w. j  a v a  2  s. co  m*/
public static void removeAllChildren(Element node, String elementName) {
    if (node != null) {
        NodeList nl = node.getChildNodes();
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = nl.item(i);
            if (childNode != null && childNode.getLocalName() != null
                    && childNode.getLocalName().equals(elementName))
                node.removeChild(childNode);
        }
    }
}

From source file:Main.java

@Deprecated
private static void removeWhitespaceNodes(org.w3c.dom.Element e) {
    org.w3c.dom.NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        org.w3c.dom.Node child = children.item(i);
        if ((child instanceof org.w3c.dom.Text)
                && (((org.w3c.dom.Text) child).getData().trim().length() == 0)) {
            e.removeChild(child);
        } else if (child instanceof org.w3c.dom.Element) {
            removeWhitespaceNodes((org.w3c.dom.Element) child);
        }//from w  w w.j  a v a 2  s  .c  o m
    }
}

From source file:Main.java

public static void genericRemoveAll(Element parent, String tagname) {
    NodeList nl = parent.getChildNodes();
    List<Element> parts = new ArrayList<Element>();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof Element) {
            Element elem = (Element) nl.item(i);
            if (elem.getTagName().equals(tagname))
                parts.add(elem);/*from  www  .  jav  a  2s .  c  o m*/
        }
    }
    for (Element part : parts)
        parent.removeChild(part);
}

From source file:Main.java

public static void stripWhitespace(Element element) {
    final NodeList childNodeList = element.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node node = childNodeList.item(i);
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            final String text = ((CharacterData) node).getData();
            if (WhitespacePattern.matcher(text).matches()) {
                element.removeChild(node);
                --i;/* w  w w .  j a  va2s .  com*/
            }
            break;
        case Node.ELEMENT_NODE:
            stripWhitespace((Element) node);
            break;
        }
    }
}

From source file:Main.java

public static void removeNodes(Element parent, NodeList elements) {
    // Copy elements in case of NodeList is a live implementation
    // that would shrink while deleting elments
    List<Node> nodesToRemove = new ArrayList<Node>();

    for (int i = 0; i < elements.getLength(); i++) {
        Node item = elements.item(i);

        if (item.getParentNode() == parent) {
            nodesToRemove.add(item);//from  w w w . j a  va  2  s  .c  om
        }
    }

    for (Node n : nodesToRemove) {
        parent.removeChild(n);
    }
}

From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java

/**
 * Moves event processing elements from previous locations into new element.
 * /*from   w ww.  ja  v a2 s.  c  om*/
 * @param config
 * @param dcomm
 * @param eproc
 * @param document
 * @throws SiteWhereException
 */
protected static void moveEventProcessingElements(Element config, Element dcomm, Element eproc,
        Document document) throws SiteWhereException {
    Element iproc = DomUtils.getChildElementByTagName(dcomm, "inbound-processing-strategy");
    if (iproc != null) {
        dcomm.removeChild(iproc);
        eproc.appendChild(iproc);
    }
    Element ichain = DomUtils.getChildElementByTagName(config, "inbound-processing-chain");
    if (ichain != null) {
        config.removeChild(ichain);
        eproc.appendChild(ichain);
    }
    Element oproc = DomUtils.getChildElementByTagName(dcomm, "outbound-processing-strategy");
    if (oproc != null) {
        dcomm.removeChild(oproc);
        eproc.appendChild(oproc);
    }
    Element ochain = DomUtils.getChildElementByTagName(config, "outbound-processing-chain");
    if (ochain != null) {
        config.removeChild(ochain);
        eproc.appendChild(ochain);
    }
    Element reg = DomUtils.getChildElementByTagName(dcomm, "registration");
    if (reg != null) {
        String qname = (reg.getPrefix() != null) ? (reg.getPrefix() + ":" + "device-services")
                : "device-services";
        document.renameNode(reg, reg.getNamespaceURI(), qname);
    }
}

From source file:no.kantega.commons.util.XMLHelper.java

public static void removeChild(Element parent, String name) {
    NodeList children = parent.getChildNodes();
    if (children == null)
        return;/*ww  w .  j  a  v  a  2s.com*/

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            parent.removeChild(child);
        }
    }
}

From source file:Main.java

/**
 * Removes the child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null.//from  w  w  w. j av a2  s . c o m
 * @return the removed child element inside element, or
 * <code>null</code> if the child element is not found.
 */
public static Element removeChildElement(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    Element cur = element;
    int len = list.getLength();
    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n != null) {
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getNodeName())) {
                    cur = (Element) element.removeChild(n);
                }
            }
        }
    }
    return cur;
}

From source file:Main.java

public static Element removeElementContentWhitespace(Element root) {
    boolean foundElt = false;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        foundElt = true;//from  w  ww. j a  v a  2  s  . c o  m
        removeElementContentWhitespace(Element.class.cast(n1));
    }
    if (foundElt) {
        Node n1 = root.getFirstChild();
        while (n1 != null) {
            Node n2 = n1.getNextSibling();
            if (n1.getNodeType() == Node.TEXT_NODE && isEmptyText(Text.class.cast(n1))) {
                root.removeChild(n1);
            }
            n1 = n2;
        }
    }
    return root;
}