Java XML Node Replace replaceNode(Node masterNode, Node oldNode, Node newNode)

Here you can find the source of replaceNode(Node masterNode, Node oldNode, Node newNode)

Description

Replaces all occurrences of oldNode in masterNode with newNode.

License

Open Source License

Declaration

public static void replaceNode(Node masterNode, Node oldNode, Node newNode) 

Method Source Code

//package com.java2s;
/*/*  w w  w  .ja v a2  s  . co  m*/
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010 Technische Universitaet Berlin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Vector;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Replaces all occurrences of <code>oldNode</code> in
     * <code>masterNode</code> with <code>newNode</code>. Only nodes
     * without children are replaced!
     */
    public static void replaceNode(Node masterNode, Node oldNode, Node newNode) {
        for (int i = 0; i < masterNode.getChildNodes().getLength(); i++) {
            Node n = masterNode.getChildNodes().item(i);
            replaceNode(n, oldNode, newNode);
            if (n.getNodeType() == Node.TEXT_NODE && oldNode.getNodeType() == Node.TEXT_NODE
                    && n.getNodeValue().equals(oldNode.getNodeValue())) {
                masterNode.insertBefore(newNode.cloneNode(true), n);
                masterNode.removeChild(n);
            } else if (n.getNodeType() != Node.TEXT_NODE && n.getNodeName().equals(oldNode.getNodeName())
                    && !n.hasChildNodes()) {
                masterNode.insertBefore(newNode.cloneNode(true), n);
                masterNode.removeChild(n);
            }
        }
    }

    /**
     * Returns an array of all child nodes of <code>parentNode</code> with the given node name or
     * an empty list if no such child nodes exist. If the node name is <code>null</code> every
     * non-text and non-comment node will be returned.
     * 
     * @param parentNode a node
     * @param childNodeName node name of child nodes, may be <code>null</code>
     * @return a list of nodes
     */
    public static Node[] getChildNodes(Node parentNode, String childNodeName) {
        Vector children = new Vector();
        NodeList nodes = parentNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node n = nodes.item(i);
            if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.COMMENT_NODE)
                continue;
            if (childNodeName == null || n.getNodeName().equals(childNodeName))
                children.add(n);
        }
        Node[] result = new Node[children.size()];
        children.toArray(result);
        return result;
    }
}

Related

  1. replaceComma(Node node)
  2. replaceNode(Node oldNode, Node newNode, Node source)
  3. replaceText(Node node, String text)
  4. replaceVariable(final Node node, final String sVar, final String sValue)
  5. replaceVariable(final Node node, final String var, final String valueString)