Example usage for org.w3c.dom Node removeChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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 Node removeChildren(Node parentNode) {
    if (parentNode != null) {
        NodeList list = parentNode.getChildNodes();
        if (list != null) {
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (node != null) {
                    parentNode.removeChild(node);
                }/*from  www  . ja v  a  2 s.com*/
            }
        }
    }
    return parentNode;
}

From source file:Main.java

public static void removeAllChilderenWithoutHeader(Node node, int remainChildCount) {
    NodeList childNodes = node.getChildNodes();
    List<Node> removeNodeList = new ArrayList<Node>();
    for (int i = remainChildCount; i < childNodes.getLength(); i++) {
        removeNodeList.add(childNodes.item(i));
    }/*  www  .  jav  a  2s.  c o m*/

    for (Node childNode : removeNodeList) {
        node.removeChild(childNode);
    }

}

From source file:Main.java

/**
 * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree.
 *//*w ww . j  av  a2  s.  c  o  m*/
public static void stripEmptyTextNodes(Node n) {
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node c = children.item(i);
        if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE
                && c.getTextContent().trim().length() == 0) {
            n.removeChild(c);
            i--;
            length--;
            children = n.getChildNodes();
        } else {
            stripEmptyTextNodes(c);
        }
    }
}

From source file:Main.java

public static void replaceChild(Document doc, Node parentNode, String childName, String childContents) {
    Node node = getChildNode(parentNode, childName);

    // if some one passes in null
    if (childContents == null) {
        // remove existing node
        if (node != null)
            parentNode.removeChild(node);

        return;//from   ww w. j a va2  s  .c  o  m
    }

    // this means there's at least contents pass in,
    if (node == null) {
        // no existing node, so we just add one
        appendChild(doc, parentNode, childName, childContents);
    } else {
        // existing node so we update it instead.
        node.setTextContent(childContents);
    }
}

From source file:Main.java

/**
 * Removes any child elements that match the type
 * @param parent The element whose children will be removed
 * @param type The name of the children elements to remove
 *///  w  ww  .  j a  v a  2 s  .com
public static void removeChildrenOfType(final Node parent, final String type) {
    final NodeList children = parent.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
        final Node child = children.item(childIndex);
        if (child.getNodeName().equals(type)) {
            parent.removeChild(child);
            break;
        }
    }
}

From source file:Main.java

/**
 * removes any immediate child nodes with the given name.
 *///www . j  av a 2  s  .  c om
public static void removeAllChildNodes(Node node, String name) {
    // this must be done as a 2 pass algorithm. 
    // attempt at doing it at single pass fails because the list is 
    // being altered as we read it which leads to errors.
    ArrayList<Node> removeList = getChildNodes(node, name);

    for (Node temp : removeList) {
        node.removeChild(temp);
    }
}

From source file:XMLUtils.java

public static void removeContents(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        parent.removeChild(node);
        node = node.getNextSibling();/*www  .  java2s.  co  m*/
    }
}

From source file:com.betfair.testing.utils.cougar.assertions.AssertionUtils.java

private static void doDomSorting(Document doc, XPath xpath, String x)
        throws XPathExpressionException, IOException {
    NodeList parentNodes = (NodeList) xpath.evaluate(x, doc, XPathConstants.NODESET);
    for (int i = 0; i < parentNodes.getLength(); i++) {
        Node n = parentNodes.item(i);
        List<Node> allKids = new ArrayList<>(n.getChildNodes().getLength());
        for (int j = n.getChildNodes().getLength() - 1; j >= 0; j--) {
            allKids.add(n.removeChild(n.getFirstChild()));
        }/*from   w  w  w  . jav a2 s.c om*/
        final Map<Node, String> kidsToString = new HashMap<>();

        for (Node k : allKids) {
            kidsToString.put(k, toString(k));
        }
        Collections.sort(allKids, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return kidsToString.get(o1).compareTo(kidsToString.get(o2));
            }
        });
        for (Node k : allKids) {
            n.appendChild(k);
        }
    }
}

From source file:Main.java

public static void removeAllSubNodesExceptAttributes(Node n) {
    NodeList childNodes = n.getChildNodes();
    List<Node> childNodesToRemove = new ArrayList<Node>();

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

        if (c.getNodeType() != Node.ATTRIBUTE_NODE) {
            childNodesToRemove.add(c);//from   www  .java2 s. c om
        }
    }

    for (Node c : childNodesToRemove) {
        n.removeChild(c);
    }
}

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        Node nextChild = childNode.getNextSibling();
        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }//w  ww.java 2 s.  c  om
        }
        childNode = nextChild;
    }
}