List of utility methods to do XML Document Clone
Document | copyDomDocument(Document originalDomDoc) copy Dom Document DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Node originalRoot = originalDomDoc.getDocumentElement();
Document copiedDocument = db.newDocument();
Node copiedRoot = copiedDocument.importNode(originalRoot, true);
copiedDocument.appendChild(copiedRoot);
return copiedDocument;
|
Element | copyElement(Element orig, Document newOwner, Set Makes a copy of the tree rooted at orig , considering only types Element and Attr .
String tagName = orig.getTagName(); Element e = newOwner.createElement(tagName); NamedNodeMap atts = orig.getAttributes(); for (int i = 0, len = atts.getLength(); i < len; ++i) { Attr origAttr = (Attr) atts.item(i); Attr newAttr = newOwner.createAttribute(origAttr.getName()); newAttr.setValue(origAttr.getValue()); e.setAttributeNode(newAttr); ... |
Node | copyNode(Document new_doc, Node node) copy Node Node new_node; switch (node.getNodeType()) { case Node.ELEMENT_NODE: new_node = new_doc.createElementNS(node.getNamespaceURI(), node.getNodeName()); copyChildren(new_doc, node, new_node); return new_node; case Node.TEXT_NODE: new_node = new_doc.createTextNode(((Text) node).getData()); ... |
Node | copyNode(final Document newDoc, final Node node) copy Node final Node newNode; switch (node.getNodeType()) { case Node.ELEMENT_NODE: newNode = newDoc.createElementNS(node.getNamespaceURI(), node.getNodeName()); final NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { final Attr attr = (Attr) attributes.item(i); final Attr newAttr = newDoc.createAttributeNS(attr.getNamespaceURI(), attr.getNodeName()); ... |
Document | deepCloneDocument(Document doc, DOMImplementation impl) Deep clones a document using the given DOM implementation. Element root = doc.getDocumentElement(); Document result = impl.createDocument(root.getNamespaceURI(), root.getNodeName(), null); Element rroot = result.getDocumentElement(); boolean before = true; for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) { if (n == root) { before = false; if (root.hasAttributes()) { ... |
void | deepCopyDocument(Document ttml, File target) deep Copy Document try { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("//*/@backgroundImage"); NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { Node backgroundImage = nl.item(i); URI backgroundImageUri = URI.create(backgroundImage.getNodeValue()); ... |