Java XML NodeList replace(Node src, NodeList nodes)

Here you can find the source of replace(Node src, NodeList nodes)

Description

replace

License

BSD License

Parameter

Parameter Description
src a parameter
nodes a parameter

Return

the first remplaced Node

Declaration

public static Node replace(Node src, NodeList nodes) 

Method Source Code

//package com.java2s;
//License from project: BSD License 

import java.util.ArrayList;

import org.w3c.dom.Document;

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

public class Main {
    /**//from   w  ww . j a v  a 2  s .co m
     * 
     * @param src
     * @param nodes
     * @return the first remplaced Node
     */
    public static Node replace(Node src, NodeList nodes) {
        Document doc = src.getOwnerDocument();
        Node parent = src.getParentNode();

        ArrayList<Node> copy = new ArrayList<Node>();
        for (int i = 0; i < nodes.getLength(); i++)
            copy.add(nodes.item(i));

        Node sibling = src.getNextSibling();
        parent.removeChild(src);
        Node last = sibling;
        Node first = null;
        if (sibling == null) {
            for (Node n : copy) {
                last = parent.appendChild(doc.adoptNode(n));
                if (first == null)
                    first = last;
            }
        } else {
            for (Node n : copy) {
                last = parent.insertBefore(doc.adoptNode(n), sibling);
                if (first == null)
                    first = last;
            }
        }
        return first;
    }
}

Related

  1. printNode(NodeList nodeList, int level)
  2. printNodeList(NodeList nodes)
  3. printNodeTypes(NodeList rows)
  4. removeBlankTextNode(NodeList nodes)
  5. removeNodeListContent(NodeList nodeList)
  6. replaceElement(Element oldElement, NodeList newNodes)
  7. replaceElement(Element oldElement, NodeList newNodes)
  8. searchDialogByName(NodeList nodeList, String id)
  9. selectElement(NodeList nodeList, String name)