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:com.enonic.esl.xml.XMLTool.java

private static Node moveNode(Node node, Node toParent) {
    Node fromParent = node.getParentNode();
    fromParent.removeChild(node);
    return toParent.appendChild(node);
}

From source file:isl.FIMS.utils.Utils.java

public static Node removeNode(Node root, String elementName, boolean deep) {
    if (!(root.hasChildNodes())) {
        return null;
    }/*from w ww .  java  2s. com*/

    Node matchingNode = null;
    String nodeName = null;
    Node child = null;

    NodeList childNodes = root.getChildNodes();
    int noChildren = childNodes.getLength();
    for (int i = 0; i < noChildren; i++) {
        if (matchingNode == null) {
            child = childNodes.item(i);
            nodeName = child.getNodeName();
            if ((nodeName != null) & (nodeName.equals(elementName))) {
                root.removeChild(child);
                return child;
            }
            if (deep) {
                matchingNode = removeNode(child, elementName, deep);
            }
        } else {
            break;
        }
    }

    return matchingNode;
}

From source file:DomUtils.java

/**
 * Insert the supplied node before the supplied reference node (refNode).
 * @param newNode Node to be inserted.//from w w w  . j av a 2s  . co  m
 * @param refNode Reference node before which the supplied nodes should
 * be inserted.
 */
public static void insertBefore(Node newNode, Node refNode) {

    Node parentNode = refNode.getParentNode();

    if (parentNode == null) {
        System.out.println(
                "Cannot insert [" + newNode + "] before [" + refNode + "]. [" + refNode + "] has no parent.");
        return;
    }

    if (parentNode instanceof Document && newNode.getNodeType() == Node.ELEMENT_NODE) {
        System.out.println(
                "Request to insert an element before the Document root node.  This is not allowed.  Replacing the Document root with the new Node.");
        parentNode.removeChild(refNode);
        parentNode.appendChild(newNode);
    } else {
        parentNode.insertBefore(newNode, refNode);
    }
}

From source file:aurelienribon.gdxsetupui.ProjectUpdate.java

private void writeGwtDefinition(File gwtDefitionFile, List<GwtModule> modules) {
    try {/*from www .  j av a  2s .  com*/
        Document doc = XmlUtils.createParser().parse(gwtDefitionFile);
        Node root = (Node) XmlUtils.xpath("module", doc, XPathConstants.NODE);
        NodeList nodes = (NodeList) XmlUtils.xpath("module/inherits", doc, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            root.removeChild(nodes.item(i));
        }

        for (GwtModule module : modules) {
            Element elem = doc.createElement("inherits");
            root.appendChild(elem);

            elem.setAttribute("name", module.name);
        }

        XmlUtils.clean(doc);
        String str = XmlUtils.transform(doc);
        FileUtils.writeStringToFile(gwtDefitionFile, str);

    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.photon.phresco.framework.impl.ConfigurationWriter.java

public void updateTestConfiguration(SettingsInfo settingsInfo, String browser, String resultConfigXml)
        throws PhrescoException {
    try {/*from   w  w  w.  j a  v a 2s. c om*/
        Node configNode = getNode("environment");
        Node node = getNode("environment/" + settingsInfo.getType());
        Node browserNode = getNode("environment/Browser");
        if (node != null) {
            configNode.removeChild(node);
        }

        if (browserNode != null) {
            browserNode.setTextContent(browser);
        } else {
            Element browserEle = getDocument().createElement("Browser");
            browserEle.setTextContent(browser);
            configNode.appendChild(browserEle);
        }
        configNode.appendChild(createConfigElement(settingsInfo));
    } catch (Exception e) {
        throw new PhrescoException("Configuration not found to delete");
    }
}

From source file:cc.siara.csv_ml.ParsedObject.java

/**
 * Performas any pending activity against an element to finalize it. In this
 * case, it adds any pending attributes needing namespace mapping.
 * /*from   ww w.  j  a va 2 s .c om*/
 * Also if the node has a namespace attached, it recreates the node with
 * specific URI.
 */
public void finalizeElement() {
    // Add all remaining attributes
    for (String col_name : pendingAttributes.keySet()) {
        String value = pendingAttributes.get(col_name);
        int cIdx = col_name.indexOf(':');
        String ns = col_name.substring(0, cIdx);
        String nsURI = nsMap.get(ns);
        if (nsURI == null)
            nsURI = generalNSURI;
        Attr attr = doc.createAttributeNS(nsURI, col_name.substring(cIdx + 1));
        attr.setPrefix(ns);
        attr.setValue(value);
        ((Element) cur_element).setAttributeNodeNS(attr);
    }
    // If the element had a namespace prefix, it has to be recreated.
    if (!currentElementNS.equals("") && !doc.getDocumentElement().equals(cur_element)) {
        Node parent = cur_element.getParentNode();
        Element cur_ele = (Element) parent.removeChild(cur_element);
        String node_name = cur_ele.getNodeName();
        String nsURI = nsMap.get(currentElementNS);
        if (nsURI == null)
            nsURI = generalNSURI;
        Element new_node = doc.createElementNS(nsURI, currentElementNS + ":" + node_name);
        parent.appendChild(new_node);
        // Add all attributes
        NamedNodeMap attrs = cur_ele.getAttributes();
        while (attrs.getLength() > 0) {
            Attr attr = (Attr) attrs.item(0);
            cur_ele.removeAttributeNode(attr);
            nsURI = attr.getNamespaceURI();
            new_node.setAttributeNodeNS(attr);
        }
        // Add all CData sections
        NodeList childNodes = cur_ele.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.CDATA_SECTION_NODE)
                new_node.appendChild(node);
        }
        cur_element = new_node;
    }
    pendingAttributes = new Hashtable<String, String>();
}

From source file:de.juwimm.cms.util.SmallSiteConfigReader.java

public void saveValues(String xmlPath, List<String> values) {
    String[] pathComponents = xmlPath.split("/");
    String itemTag = pathComponents[pathComponents.length - 1];
    Node parentNode = buildXmlPathToLeaf(xmlPath);

    if (parentNode == null) {
        return;/*from  ww  w. j  a  va  2 s. c o  m*/
    }
    //remove old values
    int oldLength = parentNode.getChildNodes().getLength();
    for (int i = 0; i < oldLength; i++) {
        parentNode.removeChild(parentNode.getChildNodes().item(0));
    }

    if (values == null || values.size() == 0) {
        return;
    }

    //save new values
    Document document = this.getConfdoc();
    for (String value : values) {
        Node valueNode = document.createElement(itemTag);
        valueNode.appendChild(document.createTextNode(value));
        parentNode.appendChild(valueNode);
    }
}

From source file:aurelienribon.gdxsetupui.ProjectUpdate.java

private void writeClasspath(File classpathFile, List<ClasspathEntry> classpath) {
    try {/*ww w  .ja va2  s.co  m*/
        Document doc = XmlUtils.createParser().parse(classpathFile);
        Node root = (Node) XmlUtils.xpath("classpath", doc, XPathConstants.NODE);
        NodeList libsNodes = (NodeList) XmlUtils.xpath("classpath/classpathentry[@kind='lib' and @path]", doc,
                XPathConstants.NODESET);

        for (int i = 0; i < libsNodes.getLength(); i++) {
            root.removeChild(libsNodes.item(i));
        }

        for (ClasspathEntry entry : classpath) {
            Element elem = doc.createElement("classpathentry");
            root.appendChild(elem);

            elem.setAttribute("kind", "lib");
            if (entry.exported)
                elem.setAttribute("exported", "true");
            elem.setAttribute("path", entry.path);
            if (entry.sourcepath != null)
                elem.setAttribute("sourcepath", entry.sourcepath);
        }

        XmlUtils.clean(doc);
        String str = XmlUtils.transform(doc);
        FileUtils.writeStringToFile(classpathFile, str);

    } catch (SAXException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.enonic.esl.xml.XMLTool.java

public static void removeChildNodes(Node root) {
    if (root == null) {
        return;//w  w w .  jav  a 2s .  co m
    }

    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength();) {
        root.removeChild(nodeList.item(i));
    }
}

From source file:de.juwimm.cms.util.SmallSiteConfigReader.java

public void saveValue(String xmlPath, String value) {
    String[] pathComponents = xmlPath.split("/");
    String itemTag = pathComponents[pathComponents.length - 1];
    Node parentNode = buildXmlPathToLeaf(xmlPath);
    if (parentNode == null) {
        return;/*from ww  w  .  j av  a 2s  .c  o  m*/
    }

    if (value == null) {
        return;
    }

    //delete old value
    try {
        Node oldNode = XercesHelper.findNode(parentNode, "//" + itemTag);
        parentNode.removeChild(oldNode);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("SmalSiteConfigReader: old value at path " + xmlPath + " does not exist");
        }
    }
    //create new value
    Document document = this.getConfdoc();
    Node valueNode = document.createElement(itemTag);
    valueNode.appendChild(document.createTextNode(value));
    parentNode.appendChild(valueNode);

}