Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class Main {
    public static Node duplicate(Node node, Document ownerDocument) {
        if (node instanceof Text)
            return ownerDocument.createTextNode(node.getNodeValue());
        Node newNode = ownerDocument.createElement(node.getNodeName());
        NamedNodeMap attribs = node.getAttributes();
        for (int i = 0; i < attribs.getLength(); i++) {
            Node attrib = attribs.item(i);
            addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
        }
        for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
            Node newN = duplicate(n, ownerDocument);
            newNode.appendChild(newN);
        }
        return newNode;
    }

    public static Attr addAttribute(Node parent, String name, String value) {
        Attr node = parent.getOwnerDocument().createAttribute(name);
        node.setValue(value);
        parent.getAttributes().setNamedItem(node);
        return node;
    }
}