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:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Removes elements by a specified tag name from the xml Element passed in.
 * @param doc The xml Element to remove from.
 * @param tagname The tag name to remove.
 *//*from  w w  w.j a  va  2  s .c  o m*/
public static void removeNodesByTagName(Element doc, String tagname) {
    NodeList nodes = doc.getElementsByTagName(tagname);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        doc.removeChild(n);
    }
}

From source file:DOMUtils.java

private static void trimEmptyTextNodes(Node node) {
    Element element = null;
    if (node instanceof Document) {
        element = ((Document) node).getDocumentElement();
    } else if (node instanceof Element) {
        element = (Element) node;
    } else {/*from  w  w w  . ja  v a2  s  .  c  om*/
        return;
    }

    List<Node> nodesToRemove = new ArrayList<Node>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            trimEmptyTextNodes(child);
        } else if (child instanceof Text) {
            Text t = (Text) child;
            if (t.getData().trim().length() == 0) {
                nodesToRemove.add(child);
            }
        }
    }

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

From source file:org.apache.lens.regression.util.Util.java

public static void changeConfig(HashMap<String, String> map, String remotePath) throws Exception {

    Path p = Paths.get(remotePath);
    String fileName = p.getFileName().toString();
    backupFile = localFilePath + "backup-" + fileName;
    localFile = localFilePath + fileName;
    log.info("Copying " + remotePath + " to " + localFile);
    remoteFile("get", remotePath, localFile);
    Files.copy(new File(localFile).toPath(), new File(backupFile).toPath(), REPLACE_EXISTING);

    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = docBuilder.parse(new FileInputStream(localFile));
    doc.normalize();//from ww w  .  j  av  a2s  .c  o  m

    NodeList rootNodes = doc.getElementsByTagName("configuration");
    Node root = rootNodes.item(0);
    Element rootElement = (Element) root;
    NodeList property = rootElement.getElementsByTagName("property");

    for (int i = 0; i < property.getLength(); i++) { //Deleting redundant properties from the document
        Node prop = property.item(i);
        Element propElement = (Element) prop;
        Node propChild = propElement.getElementsByTagName("name").item(0);

        Element nameElement = (Element) propChild;
        if (map.containsKey(nameElement.getTextContent())) {
            rootElement.removeChild(prop);
            i--;
        }
    }

    Iterator<Entry<String, String>> ab = map.entrySet().iterator();
    while (ab.hasNext()) {
        Entry<String, String> entry = ab.next();
        String propertyName = entry.getKey();
        String propertyValue = entry.getValue();
        System.out.println(propertyName + " " + propertyValue + "\n");
        Node newNode = doc.createElement("property");
        rootElement.appendChild(newNode);
        Node newName = doc.createElement("name");
        Element newNodeElement = (Element) newNode;

        newName.setTextContent(propertyName);
        newNodeElement.appendChild(newName);

        Node newValue = doc.createElement("value");
        newValue.setTextContent(propertyValue);
        newNodeElement.appendChild(newValue);
    }
    prettyPrint(doc);
    remoteFile("put", remotePath, localFile);
}

From source file:com.viadee.acceptancetests.roo.addon.PluginDependency.java

private void removeAllChildNodes(Element element) {
    while (element.hasChildNodes()) {
        element.removeChild(element.getFirstChild());
    }//from  ww  w. j a  va2 s .  co m
}

From source file:com.weaforce.system.component.fckeditor.response.XmlResponse.java

/**
 * Lists all folders in the given dir as XML tags.
 * @param dir/*from w ww .  ja  va 2  s. c om*/
 */
public void setFolders(File dir) {

    if (foldersElement != null) {
        Element parent = (Element) foldersElement.getParentNode();
        parent.removeChild(foldersElement);
    }

    foldersElement = document.createElement("Folders");
    document.getDocumentElement().appendChild(foldersElement);

    String[] fileList = dir.list(DirectoryFileFilter.DIRECTORY);
    for (String file : fileList) {
        Element folderElement = document.createElement("Folder");
        folderElement.setAttribute("name", file);
        foldersElement.appendChild(folderElement);
    }
}

From source file:com.weaforce.system.component.fckeditor.response.XmlResponse.java

/**
 * Lists all files in the given dir as XML tags.
 * /*from  ww  w  . j  a va  2  s.  co  m*/
 * @param dir
 */
public void setFiles(File dir) {

    if (filesElement != null) {
        Element parent = (Element) filesElement.getParentNode();
        parent.removeChild(filesElement);
    }

    filesElement = document.createElement("Files");
    document.getDocumentElement().appendChild(filesElement);

    File[] fileList = dir.listFiles((FileFilter) FileFileFilter.FILE);
    long length;
    for (File file : fileList) {
        Element fileElement = document.createElement("File");
        fileElement.setAttribute("name", file.getName());
        if (file.length() < 1024)
            length = 1L;
        else
            length = file.length() / 1024;
        fileElement.setAttribute("size", String.valueOf(length));
        filesElement.appendChild(fileElement);
    }
}

From source file:cz.mzk.editor.server.fedora.utils.FedoraUtils.java

/**
 * Removes the elements./*from w  ww  .j a  v  a 2s  . co m*/
 * 
 * @param parent
 *        the parent
 * @param doc
 *        the doc
 * @throws XPathExpressionException
 */
public static List<Element> removeElements(Element parent, Document doc, String xPath)
        throws XPathExpressionException {
    XPathExpression expr = makeNSAwareXpath().compile(xPath);
    NodeList nodes = null;
    List<Element> removedNodes = null;
    try {
        nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
        removedNodes = new ArrayList<Element>(nodes.getLength());
        for (int i = 0, lastIndex = nodes.getLength() - 1; i <= lastIndex; i++) {
            removedNodes.add((Element) parent.removeChild(nodes.item(i)));
        }
    } catch (XPathExpressionException e) {
        LOGGER.error("Unable to remove elements", e);
    }
    return removedNodes;
}

From source file:Main.java

/**
 * For compatibility reasons the following is required:
 * If the value of a text node is to be changed, but a CDATA section with this name
 * already exists, the CDATA section is removed an a text node is created or changed.
 *
 * If the value of a CDATA section is to be changed, but a text node with this name
 * already exists, the text node is removed an a CDATA section is created or changed.
 *
 */// www .ja v a  2 s.com
public static void setElementText(Element e, String newValue, boolean cdata) {
    if (e == null) {
        return;
    }

    Node node = null;

    NodeList children = e.getChildNodes();

    if (children != null) {
        Node childToRemove = null;
        boolean changed = false;

        int listLength = children.getLength();

        for (int i = 0; i < listLength; i++) {
            node = children.item(i);

            int nodeType = node.getNodeType();

            if (nodeType == Node.TEXT_NODE) {
                if (cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }
            }

            if (nodeType == Node.CDATA_SECTION_NODE) {
                if (!cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }

            }
        }

        if (childToRemove != null) {
            // System.out.println("removing child " + childToRemove.getNodeValue());
            childToRemove.setNodeValue("");
            e.removeChild(childToRemove);
        }

        if (changed) {
            return;
        }
    }

    Document doc = e.getOwnerDocument();

    if (cdata) {
        node = doc.createCDATASection(newValue);
    } else {
        node = doc.createTextNode(newValue);
    }

    e.appendChild(node);
}

From source file:br.gov.lexml.parser.documentoarticulado.LexMLParserFromText.java

private String removeNotParsedParagraphs(String xml) {
    String retorno = null;//from  w  ww .  j av a  2  s . com
    Document doc = LexMLUtil.toDocument(xml);
    Element root = doc.getDocumentElement();
    NodeList nodelist = root.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++)
        if (nodelist.item(i).getNodeName().equals(TAG_PARAGRAPH)) {
            root.removeChild(nodelist.item(i));
            i--;
        }
    if (getDataLocalFecho() == null)
        retorno = LexMLUtil.xmlToString(doc);
    else
        retorno = LexMLUtil.xmlToString(doc).replace(getDataLocalFecho(), "")
                .replace(getAssinatura().toString().replace(",", "").replace("[", "").replace("]", ""), "");
    return retorno;
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * Appends the specified value as a text node to the element. If the
 * value is <code>null</code>, the element's first child node will be
 * removed.// w  w  w  . j  a  v a  2  s .  c  o m
 *
 * @param element the element.
 * @param value   the element's value.
 */
public static void setElementValue(Element element, String value) {
    Node child = element.getFirstChild();

    if (value != null) {
        if (child == null) {
            child = element.getOwnerDocument().createTextNode("");
            element.appendChild(child);
        }

        child.setNodeValue(value);
    } else {
        if (child != null) {
            element.removeChild(child);
        }
    }
}