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.DocumentFragment;
import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * Will create a documentFragment of the replacingDocument, will import the
     * replacingDocument as a node of the replacedDocument, and then will
     * replace the replaceNode with the documentFragment of replacingDocument.
     * 
     * @param replacedDocument
     *            The document which will have a node replace
     * @param replacingDocument
     *            The document that will replace a node
     * @param replacedNode
     *            The node in replacedDocument that will be replaced
     * @return The new version of replacedDocument will replacedNode replaced
     */
    public static Node replaceNode(Document replacedDocument, Document replacingDocument, Node replacedNode) {

        // Create a documentFragment of the replacingDocument
        DocumentFragment docFrag = replacingDocument.createDocumentFragment();
        Element rootElement = replacingDocument.getDocumentElement();
        docFrag.appendChild(rootElement);

        // Import docFrag under the ownership of replacedDocument
        Node replacingNode = ((replacedDocument).importNode(docFrag, true));

        // In order to replace the node need to retrieve replacedNode's parent
        Node replaceNodeParent = replacedNode.getParentNode();
        replaceNodeParent.replaceChild(replacingNode, replacedNode);
        return replacedDocument;
    }

    public static Node replaceNode(Document ImpD, Node oldE, Node newE) {
        Node Parent = oldE.getParentNode();
        Node ImpNewNode = ImpD.importNode(newE, true);
        Parent.replaceChild(ImpNewNode, oldE);

        return Parent;
    }
}