public static Node extractNode(Node parent, Node node) throws MalformedElementException {

        // Get a handle to the root of the parent node tree.
        Document document;
        if (parent.getNodeType() == Node.DOCUMENT_NODE)
            document = (Document) parent;
        else
            document = parent.getOwnerDocument();

        // Create a clone of the node owned by the document, and add to the parent.
        Node nodeClone = cloneNode(document, node);
        parent.appendChild(nodeClone);

        // If the node is an Element
        if (node.getNodeType() == Node.ELEMENT_NODE) {

            // Figure out its namespace information.
            String nsPrefix = getNSPrefix((Element) node);
            String nsName = getNSName((Element) node);

            // Is this namespace already defined on the clone?  If not declare it.
            String nsNameClone = resolve(nsPrefix, (Element) nodeClone);
            if (nsName != nsNameClone && (nsName == null || !nsName.equals(nsNameClone)))
                declareNS((Element) nodeClone, nsPrefix, nsName);

            // Do the same namespace fix-up for each of the node's attributes.
            NamedNodeMap nodeMap = nodeClone.getAttributes();
            for (int i = 0; i < nodeMap.getLength(); ++i) {
                Attr attr = (Attr) nodeMap.item(i);
                nsPrefix = getNSPrefix(attr.getName());
                if (nsPrefix != null && !nsPrefix.equals(XML_PREFIX)) {
                    nsName = resolve(nsPrefix, (Element) node);
                    nsNameClone = resolve(nsPrefix, (Element) nodeClone);
                    if (nsName != nsNameClone && (nsName == null || !nsName.equals(nsNameClone)))
                        declareNS((Element) nodeClone, nsPrefix, nsName);
                }
            }
        }

        // Recursively clone each of the node's children.
        Node child = node.getFirstChild();
        while (child != null) {
            extractNode(nodeClone, child);
            child = child.getNextSibling();
        }

        // Finished cloning this node.
        return nodeClone;
    }
public static Node extractNode ( Node parent , Node node ) throws MalformedElementException { Document document ; if ( parent . getNodeType ( ) == Node . DOCUMENT_NODE ) document = ( Document ) parent ; else document = parent . getOwnerDocument ( ) ; Node nodeClone = cloneNode ( document , node ) ; parent . appendChild ( nodeClone ) ; if ( node . getNodeType ( ) == Node . ELEMENT_NODE ) { String nsPrefix = getNSPrefix ( ( Element ) node ) ; String nsName = getNSName ( ( Element ) node ) ; String nsNameClone = resolve ( nsPrefix , ( Element ) nodeClone ) ; if ( nsName != nsNameClone && ( nsName == null || ! nsName . equals ( nsNameClone ) ) ) declareNS ( ( Element ) nodeClone , nsPrefix , nsName ) ; NamedNodeMap nodeMap = nodeClone . getAttributes ( ) ; for ( int i = 0 ; i < nodeMap . getLength ( ) ; ++ i ) { Attr attr = ( Attr ) nodeMap . item ( i ) ; nsPrefix = getNSPrefix ( attr . getName ( ) ) ; if ( nsPrefix != null && ! nsPrefix . equals ( XML_PREFIX ) ) { nsName = resolve ( nsPrefix , ( Element ) node ) ; nsNameClone = resolve ( nsPrefix , ( Element ) nodeClone ) ; if ( nsName != nsNameClone && ( nsName == null || ! nsName . equals ( nsNameClone ) ) ) declareNS ( ( Element ) nodeClone , nsPrefix , nsName ) ; } } } Node child = node . getFirstChild ( ) ; while ( child != null ) { extractNode ( nodeClone , child ) ; child = child . getNextSibling ( ) ; } return nodeClone ; }
Clones the given node, and its subtrees, and sets it as the root of
the given parent node. Returns the cloned node. The parent node and
the node to be cloned must not be <code>null</code>.

@param parent  the node that will become the parent of the cloned
node
@param node the node to be cloned
@returns       a clone of the element
@throws        MalformedElementException if an element exists in the
subtree to be cloned whose namespace can't be resolved