Java Utililty Methods XML Document Clone

List of utility methods to do XML Document Clone

Description

The list of methods to do XML Document Clone are organized into topic(s).

Method

voidcopy(Document from, Document to)
copy
Element fromRootElem = from.getDocumentElement();
Element toRootElem = to.getDocumentElement();
Node childNode, importedNode;
NodeList toChildNodes = fromRootElem.getChildNodes();
for (int i = 0; i < toChildNodes.getLength(); i++) {
    childNode = toChildNodes.item(i);
    importedNode = to.importNode(childNode, true);
    toRootElem.appendChild(importedNode);
...
voidcopyChildren(Document new_doc, Node node, Node new_node)
copy Children
final NodeList children = node.getChildNodes();
Node new_child;
for (int i = 0; i < children.getLength(); i++) {
    final Node child = children.item(i);
    if (child == null) {
        continue;
    switch (child.getNodeType()) {
...
voidcopyChildren(Element from, Element to, Document doc)
copy Children
Node child = from.getFirstChild();
while (child != null) {
    to.appendChild(doc.importNode(child,  true));
    child = child.getNextSibling();
voidcopyChildren(final Document newDoc, final Node node, final Node newNode)
copy Children
final NodeList children = node.getChildNodes();
Node newChild;
for (int i = 0; i < children.getLength(); i++) {
    final Node child = children.item(i);
    if (child == null) {
        continue;
    switch (child.getNodeType()) {
...
DocumentcopyDocument(Document document)
copy Document
Node n = document.getDocumentElement().cloneNode(true);
Document result = newDocument();
result.adoptNode(n);
result.appendChild(n);
return result;
voidcopyDocument(Document from, Document to)
copy Document
Node node = to.importNode(from.getDocumentElement(), true);
to.getDocumentElement().appendChild(node);
DocumentcopyDocument(Document oldDocument, Document newDocument)
Copies a document's content to another document (i.e.
Node root = oldDocument.getDocumentElement();
Node adoptedRoot = newDocument.adoptNode(root);
newDocument.appendChild(adoptedRoot);
return newDocument;
DocumentcopyDocument(Document source)
Clone a Document object.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Node originalRoot = source.getDocumentElement();
Document copiedDocument = db.newDocument();
Node copiedRoot = copiedDocument.importNode(originalRoot, true);
copiedDocument.appendChild(copiedRoot);
return copiedDocument;
...
voidcopyDocument(Element from, Element to, String newNamespace)
Copy elements from one document to another attaching at the specified element and translating the namespace.
Document doc = to.getOwnerDocument();
NodeList nl = from.getChildNodes();
int length = nl.getLength();
for (int i = 0; i < length; i++) {
    Node node = nl.item(i);
    Node newNode = null;
    if (Node.ELEMENT_NODE == node.getNodeType()) {
        Element oldElement = (Element) node;
...
voidcopyDocumentNode(Node source, Document target)
Copy a Node from one source document, adding it to the document root of a different, target Document
Node node = target.importNode(source, true);
target.getDocumentElement().appendChild(node);