Java XML Document Clone cloneNode(Document d, Node n)

Here you can find the source of cloneNode(Document d, Node n)

Description

clone Node

License

Apache License

Declaration

public static Node cloneNode(Document d, Node n) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .j  a  v a2  s. c  o m*/
 * @(#)$Id: XMLCommon.java 116 2008-10-30 06:12:51Z unsaved $
 *
 * Copyright 2008 by the JWebMail Development Team and Sebastian Schaffert.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class Main {
    public static Node cloneNode(Document d, Node n) {
        Node r = null;
        switch (n.getNodeType()) {
        case Node.TEXT_NODE:
            r = d.createTextNode(((Text) n).getData());
            break;
        case Node.CDATA_SECTION_NODE:
            r = d.createCDATASection(((CDATASection) n).getData());
            break;
        case Node.ELEMENT_NODE:
            r = d.createElement(((Element) n).getTagName());
            NamedNodeMap map = n.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
            }
            break;
        }
        return r;
    }
}

Related

  1. cloneDocument(Document document)
  2. cloneDocument(final Document doc)
  3. cloneDocument(final Document doc)
  4. cloneDOM(final Document src)
  5. cloneDOM(Node node, Document document)
  6. cloneNode(Document document, Node node, boolean deep)
  7. copy(Document from, Document to)
  8. copyChildren(Document new_doc, Node node, Node new_node)
  9. copyChildren(Element from, Element to, Document doc)