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

/**
 * Remove all text nodes below this node
 *
 * @param node The starting node for the search.
 *//*from   ww  w  .  java2  s  . co  m*/
public static void removeAllTextNodes(Node node) {
    if (node == null)
        return;
    if (!node.hasChildNodes())
        return;
    NodeList nl = node.getChildNodes();
    for (int i = nl.getLength() - 1; i >= 0; i--) {
        Node n = (Node) nl.item(i);
        if (n instanceof Text)
            node.removeChild(n);
        else
            removeAllTextNodes(n);
    }
}

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        // grab the "nextSibling" before the child node is removed
        Node nextChild = childNode.getNextSibling();

        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }/*from w  w  w  .jav a 2s .  c  om*/
        }
        childNode = nextChild;
    }
}

From source file:com.wavemaker.tools.pws.install.PwsInstall.java

public static Document insertImport(Document doc, String resource) {
    List<Node> targetList = new ArrayList<Node>();

    // First, delete old lines if any.

    NodeList list = doc.getElementsByTagName("import");
    Node node = null;/*from   w w w.jav a 2  s.com*/
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);
        NamedNodeMap attributes = node.getAttributes();
        for (int j = 0; j < attributes.getLength(); j++) {
            Node attr = attributes.item(j);
            if (attr.getNodeName().equals("resource") && attr.getNodeValue().equals(resource)) {
                targetList.add(node);
                break;
            }
        }
    }

    NodeList beans_list = doc.getElementsByTagName("beans");
    Node beans_node = beans_list.item(0);

    if (targetList.size() > 0) {
        for (Node target : targetList) {
            beans_node.removeChild(target);
        }
    }

    // Now, add the new line

    NodeList list1 = beans_node.getChildNodes();
    Node bean_node = null;
    for (int i = 0; i < list1.getLength(); i++) {
        Node node1 = list1.item(i);
        if (node1.getNodeName().equals("bean")) {
            bean_node = node1;
            break;
        }
    }

    Element elem = doc.createElement("import");
    elem.setAttribute("resource", resource);

    try {
        if (bean_node != null) {
            beans_node.insertBefore(elem, bean_node);
        } else {
            beans_node.appendChild(elem);
        }
    } catch (DOMException ex) {
        ex.printStackTrace();
    }

    return doc;
}

From source file:Main.java

/**
 * Sets the text value of an Element. if current text of the element is
 * replaced with the new text if text is null any
 * current text value is deleted./*from   www  . j a v  a  2s . c o  m*/
 * 
 * @param elem the Element for which the text value should be set
 * @param text the new text value of the element
 * @return true if the text could be set or false otherwise
 */
static public boolean setElementText(Node elem, Object text) {
    if (elem == null)
        return false; // Fehler
    // Find Text
    Node node = elem.getFirstChild();
    while (node != null) { // Find all Text nodes
        if (node.getNodeType() == Node.TEXT_NODE)
            break; // gefunden
        node = node.getNextSibling();
    }
    if (node != null) { // Set or remove text
        if (text != null)
            node.setNodeValue(text.toString());
        else
            elem.removeChild(node);
    } else if (text != null) { // Add Text
        elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString()));
    }
    return true;
}

From source file:cz.incad.kramerius.rest.api.k5.client.search.SearchResource.java

public static void filterFieldsInDOM(Element docE) {
    // filter// w ww. j a va2s  .  c o  m
    String[] filters = KConfiguration.getInstance().getAPISolrFilter();
    for (final String name : filters) {
        Element found = findSolrElement(docE, name);
        if (found != null) {
            Node parentNode = found.getParentNode();
            Node removed = parentNode.removeChild(found);
        }
    }
}

From source file:Main.java

static public Node removeChild(Node xmlNode, String name, boolean ignoreCase) {
    Node removedChild = null;/*from w ww.j a va 2s. c o  m*/

    String key = name;
    if (ignoreCase)
        key = key.toLowerCase();

    NodeList childNodes = xmlNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        String childName = child.getNodeName();
        if (ignoreCase)
            childName = childName.toLowerCase();

        if (childName.equals(key)) {
            removedChild = child;
            xmlNode.removeChild(child);
            break;
        }
    }
    return removedChild;
}

From source file:Main.java

public static void nodeSubDelete(Element root, String table, String queryType) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node deleteNode = null;/*from www .j a v a2  s  .  co m*/

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            deleteNode = n;
            System.out.println("Finding Node : " + deleteNode.getNodeName());

            break;
        }
    }

    find.removeChild(deleteNode);

    print(doc);
}

From source file:Main.java

public static void cleanText(Node node) {
    try {//from ww w  .ja va2  s.c o m
        NodeList childNodes = node.getChildNodes();
        int noChildren = childNodes.getLength();
        Node n = null;
        short type = 0;
        Vector rem = new Vector();
        for (int i = 0; i < noChildren; i++) {
            n = childNodes.item(i);
            type = n.getNodeType();
            if (type == Node.TEXT_NODE) {
                rem.add(n);
            } else if (type == Node.ELEMENT_NODE) {
                cleanText(n);
            }
        }
        for (int i = 0; i < rem.size(); i++) {
            node.removeChild((Node) rem.get(i));
        }
    } catch (Exception e) {
        //DebugUtil.debug(e);
    }
}

From source file:it.cnr.icar.eric.server.common.Utility.java

/**
 * This is an implementation of the JAXP DOM Node.setTextContent
 * method, which does not exist in JDK 1.4. This code was adapted
 * from the Apache Xerces 2.8.0 implementation of this method.
 *///from   w w w. j  ava  2  s.  com
public static void setTextContent(Node node, String textContent) throws DOMException {
    Node child;

    // get rid of any existing children
    while ((child = node.getFirstChild()) != null) {
        node.removeChild(child);
    }

    // create a Text node to hold the given content
    if (textContent != null && textContent.length() != 0) {
        node.appendChild(node.getOwnerDocument().createTextNode(textContent));
    }
}

From source file:com.mediaworx.xmlutils.XmlHelper.java

/**
 * Removes all empty or whitespace only text nodes from the given parent node.
 * @param parentNode    the parent node to be cleared of empty or whitespace only text nodes
 *//*from ww  w  .j a va 2s .  com*/
private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        // grab the "nextSibling" before the child node is removed
        Node nextChild = childNode.getNextSibling();

        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }
        }
        childNode = nextChild;
    }
}