My purpose is to read xml file into Dom object, edit the dom object, which involves removing some nodes.
After this is do开发者_如何学编程ne i wish to restore the Dom to its original state without actually parsing the XML file.
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.
You could use importNode API on org.w3c.dom.Document:
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);
}
}
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 Factory and Transformer
you could clone a tree or only the node with DOMs 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 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.
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.
<?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"?>
精彩评论