java 克隆 dom.Document 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5226852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:04:46  来源:igfitidea点击:

Cloning dom.Document object

javaxmldom

提问by Kazoom

My purpose is to read xml file into Dom object, edit the dom object, which involves removing some nodes.

我的目的是将 xml 文件读入 Dom 对象,编辑 dom 对象,这涉及删除一些节点。

After this is done i wish to restore the Dom to its original state without actually parsing the XML file.

完成此操作后,我希望将 Dom 恢复到其原始状态,而无需实际解析 XML 文件。

Is there anyway i can clone the dom object i obtained after parsing the xml file for the first time. the idea is to avoid reading and parsing xml all the time, just keep a copy of original dom tree.

无论如何我可以克隆我第一次解析xml文件后获得的dom对象。这个想法是为了避免一直读取和解析 xml,只需保留原始 dom 树的副本。

回答by bdoughan

You could use importNodeAPI on org.w3c.dom.Document:

你可以在 org.w3c.dom.Document 上使用importNodeAPI:

Node copy = document.importNode(node, true);


Full Example

完整示例

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document originalDocument = db.parse(new File("input.xml"));
        Node originalRoot = originalDocument.getDocumentElement();

        Document copiedDocument = db.newDocument();
        Node copiedRoot = copiedDocument.importNode(originalRoot, true);
        copiedDocument.appendChild(copiedRoot);

    }
}

回答by Piyush Mattoo

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer tx   = tfactory.newTransformer();
DOMSource source = new DOMSource(doc);
DOMResult result = new DOMResult();
tx.transform(source,result);
return (Document)result.getNode();

This would be the Java 1.5 solution for making a copy of the DOM document. Take a look at Transformer Factoryand Transformer

这将是用于制作 DOM 文档副本的 Java 1.5 解决方案。看看变压器工厂变压器

回答by comeGetSome

you could clone a tree or only the node with DOMs cloneNode(boolean isDeepCopy) API.

您可以使用 DOM cloneNode(boolean isDeepCopy) API 克隆一棵树或仅克隆节点。

Document originalDoc = parseDoc();
Document clonedDoc = originalDoc.cloneNode(true);

unfortunately, since cloneNode() on Document is (according to API) implementation specific, we have to go for a bullet-proof way, that is, create a new Document and import cloned node's from the original document:

不幸的是,由于 Document 上的 cloneNode() 是(根据 API)特定于实现的,我们必须采用一种防弹方法,即创建一个新的 Document 并从原始文档中导入克隆的节点:

...
Document clonedDoc = documentFactory.newDocument();
cloneDoc.appendChild(
  cloneDoc.importNode(originalDoc.getDocumentElement(), true)
);

note that none of operations are thread-safe, so either use them only locally, or Thread-Local or synchronize them.

请注意,所有操作都不是线程安全的,因此要么仅在本地使用它们,要么使用 Thread-Local 或同步它们。

回答by user2761085

I would stick with the second suggestion with TransformerFactory. With importNode you don't get a full copy of the document. The header isn't copied.

我会坚持 TransformerFactory 的第二个建议。使用 importNode 您不会获得文档的完整副本。不复制标题。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?aid style="50" type="snippet" readerVersion="6.0" featureSet="257" product="8.0(370)" ?>      
<?aid SnippetType="PageItem"?><Document DOMVersion="8.0" Self="d">

This would not return the above because this isn't copied. It's will be using what ever your new document contain.

这不会返回上述内容,因为它没有被复制。它将使用您的新文档所包含的内容。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>