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:ua.utility.kfsdbupgrade.MaintainableXMLConversionServiceImpl.java

/**
 * For each child of <code>node</code>
 * //from   w  w w. j  a v a  2s .co m
 * @param document
 * @param node
 * @param currentClass
 * @param propertyMappings
 * @throws ClassNotFoundException
 * @throws XPathExpressionException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 */
private void transformNode(Document document, Node node, Class<?> currentClass,
        Map<String, String> propertyMappings) throws ClassNotFoundException, XPathExpressionException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
    LOGGER.trace("Transforming node: " + node.getBaseURI() + "/" + node.getNodeName());
    for (Node childNode = node.getFirstChild(); childNode != null;) {
        Node nextChild = childNode.getNextSibling();
        String propertyName = childNode.getNodeName();
        if (childNode.hasAttributes()) {
            XPath xpath = XPathFactory.newInstance().newXPath();
            Node serializationAttribute = childNode.getAttributes().getNamedItem(SERIALIZATION_ATTRIBUTE);
            if (serializationAttribute != null
                    && StringUtils.equals(serializationAttribute.getNodeValue(), "custom")) {
                Node classAttribute = childNode.getAttributes().getNamedItem(CLASS_ATTRIBUTE);
                if (classAttribute != null && StringUtils.equals(classAttribute.getNodeValue(),
                        "org.kuali.rice.kns.util.TypedArrayList")) {
                    handleTypedArrayList(document, xpath, (Element) childNode);
                } else if (isTargetEffortCertificationReportPositionsNode(childNode)) {
                    // Need to skip over ECRD positions list due to needing serialization attr
                    // that otherwise was getting stripped on line 924. This also avoids a child
                    // list node from getting errantly pruned off ECRD doc types
                    deleteAllNoneListProxyChildren(childNode);
                } else {
                    ((Element) childNode).removeAttribute(SERIALIZATION_ATTRIBUTE);

                    XPathExpression mapContentsExpression = xpath.compile("//" + propertyName + "/map/string");
                    NodeList mapContents = (NodeList) mapContentsExpression.evaluate(childNode,
                            XPathConstants.NODESET);
                    List<Node> nodesToAdd = new ArrayList<Node>();
                    if (mapContents.getLength() > 0 && mapContents.getLength() % 2 == 0) {
                        for (int i = 0; i < mapContents.getLength(); i++) {
                            Node keyNode = mapContents.item(i);
                            Node valueNode = mapContents.item(++i);
                            Node entryNode = document.createElement("entry");
                            entryNode.appendChild(keyNode);
                            entryNode.appendChild(valueNode);
                            nodesToAdd.add(entryNode);
                        }
                    }
                    for (Node removeNode = childNode.getFirstChild(); removeNode != null;) {
                        Node nextRemoveNode = removeNode.getNextSibling();
                        childNode.removeChild(removeNode);
                        removeNode = nextRemoveNode;
                    }
                    for (Node nodeToAdd : nodesToAdd) {
                        childNode.appendChild(nodeToAdd);
                    }
                }
            }
        }
        if (propertyMappings != null && propertyMappings.containsKey(propertyName)) {
            String newPropertyName = propertyMappings.get(propertyName);
            if (StringUtils.isNotBlank(newPropertyName)) {
                document.renameNode(childNode, null, newPropertyName);
                propertyName = newPropertyName;
            } else {
                // If there is no replacement name then the element needs
                // to be removed and skip all other processing
                node.removeChild(childNode);
                childNode = nextChild;
                continue;
            }
        }

        if (dateRuleMap != null && dateRuleMap.containsKey(propertyName)) {
            String newDateValue = dateRuleMap.get(propertyName);
            if (StringUtils.isNotBlank(newDateValue)) {
                if (childNode.getTextContent().length() == 10) {
                    childNode.setTextContent(childNode.getTextContent() + " " + newDateValue);

                }
            }
        }

        if ((currentClass != null) && isValidClass(currentClass)) {
            if (childNode.hasChildNodes() && !(Collection.class.isAssignableFrom(currentClass)
                    || Map.class.isAssignableFrom(currentClass))) {
                PropertyClassKey key = new PropertyClassKey(currentClass, propertyName);
                Optional<Class<?>> propertyClass = propertyClassCache.getUnchecked(key);
                if (propertyClass.isPresent()
                        && classPropertyRuleMap.containsKey(propertyClass.get().getName())) {
                    transformNode(document, childNode, propertyClass.get(),
                            this.classPropertyRuleMap.get(propertyClass.get().getName()));
                }
                transformNode(document, childNode, propertyClass.orNull(), classPropertyRuleMap.get("*"));
            }
        }
        childNode = nextChild;
    }
}

From source file:ua.utility.kfsdbupgrade.MaintainableXMLConversionServiceImpl.java

private void deleteAllNoneListProxyChildren(Node ecrdPositionsNode) {
    Node childNode = ecrdPositionsNode.getFirstChild();
    while (childNode != null) {
        if (!childNode.getNodeName().equals(LIST_PROXY_NAME)) {
            // Not a list proxy node, so remove it
            ecrdPositionsNode.removeChild(childNode);
        }/*  w  w  w  . j av  a2  s.co  m*/
        childNode = childNode.getNextSibling();
    }
}

From source file:xmlconverter.controller.logic.GetFileCount.java

/**
 * This method is/are going to update the site information. The statements
 * are based on flags that are passed. This method should detect if files
 * content has changed and also update thous changed if node count has
 * changed./*from   w w w .  j  a  v a  2 s  .com*/
 *
 * @param path
 * @param pathXml
 * @throws DOMException
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void updateSite(Document newDoc, File pathXml)
        throws TransformerException, DOMException, ParserConfigurationException, IOException, SAXException {
    //System.out.println("I updated: " + pathXml.getName());
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document oldDoc = docBuilder.parse(pathXml);

    //Old root & NodeList
    Node root = oldDoc.getFirstChild();
    NodeList staff = root.getChildNodes();
    ArrayList<Node> oldNodes = new ArrayList<>();

    for (int i = 0; i < staff.getLength(); i++) {
        oldNodes.add(staff.item(i));
    }

    //New root & NodeList
    Node rootNew = newDoc.getFirstChild();
    NodeList staffNew = rootNew.getChildNodes();
    ArrayList<Node> newNodes = new ArrayList<>();

    for (int i = 0; i < staffNew.getLength(); i++) {
        newNodes.add(staffNew.item(i));
    }
    //        * All extra nodes will be removed
    //        * All not maching "id's" will be replaced
    if (oldNodes.size() > newNodes.size()) {
        for (int i = 0; i < oldNodes.size(); i++) {
            if (i >= newNodes.size()) {
                root.removeChild(oldNodes.get(i));//remove extra old nodes
            } else if (!oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue()
                    .equals(newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) {
                root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i)); //replace new node with old node
            } else {
                //System.out.println(i + "equal");
            }
        }
        //        * All not maching "id's" will be replaced
    } else if (oldNodes.size() == newNodes.size()) {
        for (int i = 0; i < oldNodes.size(); i++) {
            if (!oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue()
                    .equals(newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) {
                root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i));// replace old with new node
            } else {
                //System.out.println(i + " equal");
            }
        }
        //        * All extra nodes will be apended
        //        * All not maching "id's" will be replaced
    } else if (oldNodes.size() < newNodes.size()) {
        for (int i = 0; i < newNodes.size(); ++i) {
            if (i >= oldNodes.size()) {
                root.appendChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true))); //append extra new node
            } else if (!newNodes.get(i).getAttributes().getNamedItem("id").getNodeValue()
                    .equals(oldNodes.get(i).getAttributes().getNamedItem("id").getNodeValue())) {
                root.replaceChild(oldDoc.adoptNode(newNodes.get(i).cloneNode(true)), oldNodes.get(i));//replace old with new node
            } else {
                //System.out.println(i + "equal");
            }
        }
    }
    writeDocument(oldDoc, pathXml);
}