Example usage for org.w3c.dom Element replaceChild

List of usage examples for org.w3c.dom Element replaceChild

Introduction

In this page you can find the example usage for org.w3c.dom Element replaceChild.

Prototype

public Node replaceChild(Node newChild, Node oldChild) throws DOMException;

Source Link

Document

Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.

Usage

From source file:org.openestate.io.core.XmlUtils.java

/**
 * Replace all text values of a {@link Node} with CDATA values.
 *
 * @param doc/*  ww w  .  jav a 2s.  c o m*/
 * the document to update
 *
 * @param node
 * the node to update
 */
public static void replaceTextWithCData(Document doc, Node node) {
    if (node instanceof Text) {
        Text text = (Text) node;
        CDATASection cdata = doc.createCDATASection(text.getTextContent());
        Element parent = (Element) text.getParentNode();
        parent.replaceChild(cdata, text);
    } else if (node instanceof Element) {
        //LOGGER.debug( "ELEMENT " + element.getTagName() );
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            //LOGGER.debug( "> " + children.item( i ).getClass().getName() );
            XmlUtils.replaceTextWithCData(doc, children.item(i));
        }
    }
}

From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_2.java

/**
 * Downgrade &lt;energiepass&gt; elements to OpenImmo 1.2.1.
 * <p>//from  ww  w  .ja  va 2  s  . c  o  m
 * The &lt;epart&gt; child element of the &lt;energiepass&gt; element is
 * renamed to &lt;art&gt; in version 1.2.1.
 *
 * @param doc OpenImmo document in version 1.2.2
 * @throws JaxenException
 */
protected void downgradeEnergiepassElements(Document doc) throws JaxenException {
    List nodes = XmlUtils
            .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:epart", doc)
            .selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();

        Element newNode = doc.createElementNS(StringUtils.EMPTY, "art");
        newNode.setTextContent(node.getTextContent());

        parentNode.replaceChild(newNode, node);
    }
}

From source file:org.openestate.io.openimmo.converters.OpenImmo_1_2_2.java

/**
 * Upgrade &lt;energiepass&gt; elements to OpenImmo 1.2.2.
 * <p>/*from w w w  .  j  a  v  a  2 s  . co m*/
 * The &lt;art&gt; child element of the &lt;energiepass&gt; element is
 * renamed to &lt;epart&gt; in version 1.2.2.
 *
 * @param doc OpenImmo document in version 1.2.1
 * @throws JaxenException
 */
protected void upgradeEnergiepassElements(Document doc) throws JaxenException {
    List nodes = XmlUtils
            .newXPath("/io:openimmo/io:anbieter/io:immobilie/io:zustand_angaben/io:energiepass/io:art", doc)
            .selectNodes(doc);
    for (Object item : nodes) {
        Element node = (Element) item;
        Element parentNode = (Element) node.getParentNode();

        Element newNode = doc.createElementNS(StringUtils.EMPTY, "epart");
        newNode.setTextContent(node.getTextContent());

        parentNode.replaceChild(newNode, node);
    }
}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Adds a child node as the first child to a parent node.
 * If the first node has the same name as child node, then it 
 * is replaced.//from   www.  jav  a2 s  .c o  m
 * @param parent
 * @param child
 */
public void addFirstChild(Element parent, Element child) {
    Node firstChild = parent.getFirstChild();
    if (child.getNodeName() == firstChild.getNodeName()) {
        parent.replaceChild(child, firstChild);
    } else {
        parent.insertBefore(child, parent.getFirstChild());
    }

}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Adds a child node as the last child to a parent node
 * @param parent//from   w w  w. j  a  va  2 s  . c  om
 * @param child
 */
public void addLastChild(Element parent, Element child) {
    Node lastChild = parent.getLastChild();
    if (child.getNodeName() == lastChild.getNodeName()) {
        parent.replaceChild(child, lastChild);
    } else {
        parent.appendChild(child);
    }
}

From source file:org.smartfrog.avalanche.client.sf.apps.gt4.javawscore.utils.EditXML.java

/**
 * Adds a child node before the last child of the parent node.
 * @param parent/* www  . ja  v  a 2s .  co  m*/
 * @param child
 */
public void addBeforeLastChild(Element parent, Element child) {
    Node lastChild = parent.getLastChild();
    Node beforeLastChild = lastChild.getPreviousSibling();
    if (child.getNodeName() == beforeLastChild.getNodeName()) {
        parent.replaceChild(child, beforeLastChild);
    } else {
        parent.insertBefore(child, lastChild);
    }
}

From source file:org.wso2.carbon.apimgt.migration.client._200Specific.ResourceModifier200.java

/**
 * update new throttle handler in the synapse
 *
 * @param synapseDTOs list of synapse files to be updated
 *///from   w ww  . j  a v a2s  . c om
public static void updateThrottleHandler(List<SynapseDTO> synapseDTOs) {
    for (SynapseDTO synapseDTO : synapseDTOs) {
        Element existingThrottleHandler = SynapseUtil.getHandler(synapseDTO.getDocument(),
                Constants.SYNAPSE_API_VALUE_THROTTLE_HANDLER);
        Element handlersElement = (Element) synapseDTO.getDocument()
                .getElementsByTagNameNS(Constants.SYNAPSE_API_XMLNS, Constants.SYNAPSE_API_ELEMENT_HANDLERS)
                .item(0);
        if (existingThrottleHandler != null && !isThrottleHandlerUpdated(existingThrottleHandler)) {
            Element updatedThrottleHandler = SynapseUtil.createHandler(synapseDTO.getDocument(),
                    Constants.NEW_SYNAPSE_API_VALUE_THROTTLE_HANDLER, null);
            handlersElement.replaceChild(updatedThrottleHandler, existingThrottleHandler);
        }
    }
}