Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public static Element cloneElementAs(Element srcEl, Document dstDoc, String elName) {
        if (srcEl.getNodeName().equals(elName)) {
            if (srcEl.getOwnerDocument() == dstDoc) {
                return (Element) srcEl.cloneNode(true);
            } else {
                return (Element) dstDoc.importNode(srcEl, true);
            }
        } else {
            final Element dstEl = dstDoc.createElement(elName);
            final NodeList srcChildren = srcEl.getChildNodes();
            final int n = srcChildren.getLength();
            for (int i = 0; i < n; ++i) {
                final Node srcChild = srcChildren.item(i);
                final Node dstChild = dstDoc.importNode(srcChild, true);
                dstEl.appendChild(dstChild);
            }
            return dstEl;
        }
    }
}