List of usage examples for org.w3c.dom Node cloneNode
public Node cloneNode(boolean deep);
From source file:Main.java
public static void cloneAndAppend(Document document, Node node) { node = node.cloneNode(true); document.adoptNode(node);//from w w w . j a v a 2s. c om document.appendChild(node); }
From source file:Main.java
/** * Clone a Node/*from ww w . ja v a 2 s . com*/ * * @param node * @param isDepth * @return the new node */ public final static Node cloneNode(Node node, boolean isDepth) { return node.cloneNode(isDepth); }
From source file:Main.java
/** * @param target/*from w ww.j a va 2s .co m*/ * @param node * @return */ public static Node copyNode(Element target, Node node) { Node n = node.cloneNode(true); Document targetDoc = target.getOwnerDocument(); targetDoc.adoptNode(n); target.appendChild(n); return node; }
From source file:Main.java
private static String printNodeOnly(Node node) { return printXMLNode(node.cloneNode(false)); }
From source file:Main.java
/** * Creates a XML document with the given root element and the given {@link NodeList} as content. * @param root The root element of the XML document. * @param content Content of the XML document. * @return The created XML document.//w ww .j a v a 2s .c om */ public static Document createDocument(String root, NodeList content) { DocumentBuilder docBuilder = createDocumentBuilder(); Document document = docBuilder.newDocument(); Element rootElement = document.createElement(root); document.appendChild(rootElement); for (int i = 0; i < content.getLength(); i++) { Node item = content.item(i); item = document.adoptNode(item.cloneNode(true)); rootElement.appendChild(item); } return document; }
From source file:Main.java
public static void mergeElement(Element from, Element to) { // attrs//w w w . j a v a 2 s . com for (int idx = 0; idx < from.getAttributes().getLength(); idx++) { Node node = from.getAttributes().item(idx); to.getAttributes().setNamedItem(node.cloneNode(false)); } // children for (int idx = 0; idx < from.getChildNodes().getLength(); idx++) { Node node = from.getChildNodes().item(idx); if (!Element.class.isInstance(node)) { continue; } to.appendChild(node.cloneNode(true)); } }
From source file:Main.java
/** * Rename an element, replacing it in its document. * @param element the element to rename. * @param name the new element name.//from w ww . j a va 2 s .c om * @return the renamed element. */ public static Element renameElement(Element element, String name) { if (element.getNodeName().equals(name)) return element; Element el = element.getOwnerDocument().createElement(name); //Copy the attributes NamedNodeMap attributes = element.getAttributes(); int nAttrs = attributes.getLength(); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); el.setAttribute(attr.getNodeName(), attr.getNodeValue()); } //Copy the children Node node = element.getFirstChild(); while (node != null) { Node clone = node.cloneNode(true); el.appendChild(clone); node = node.getNextSibling(); } //Replace the element element.getParentNode().replaceChild(el, element); return el; }
From source file:Main.java
private static String getXmlString(Document document, String tagName) throws ParserConfigurationException, SAXException, IOException, TransformerException { NodeList nodeList = document.getElementsByTagName(tagName); if (nodeList.getLength() < 1) { throw new IllegalArgumentException("no " + tagName + " found"); }/*from w w w . java 2 s . co m*/ Node sub = nodeList.item(0); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document subdoc = db.newDocument(); subdoc.appendChild(sub.cloneNode(true)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(subdoc); StringWriter sw = new StringWriter(); StreamResult xmlResult = new StreamResult(sw); transformer.transform(domSource, xmlResult); sw.flush(); return sw.toString(); }
From source file:Main.java
/** * Convert an XML fragment from one namespace to another. * * @param from element to translate * @param namespace namespace to be translated to * @return the element in the new namespace * * @since 8.4// w ww . j a v a 2 s. c o m */ public static Element translateXML(Element from, String namespace) { Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName()); NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i = 0; i < length; i++) { Node node = nl.item(i); Node newNode; if (node.getNodeType() == Node.ELEMENT_NODE) { newNode = translateXML((Element) node, namespace); } else { newNode = node.cloneNode(true); } to.appendChild(newNode); } NamedNodeMap m = from.getAttributes(); for (int i = 0; i < m.getLength(); i++) { Node attr = m.item(i); to.setAttribute(attr.getNodeName(), attr.getNodeValue()); } return to; }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *//*from w ww . ja v a2 s .c o m*/ public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if (target == null || node.getOwnerDocument() == target) // same Document return node.cloneNode(deep); else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) newNode.appendChild(cloneNode(child, target, true)); return newNode; } }