List of usage examples for org.w3c.dom Document importNode
public Node importNode(Node importedNode, boolean deep) throws DOMException;
From source file:Main.java
/** * Clones a document object./* w w w . ja va 2 s . com*/ * * @param doc The document to be cloned. * @return The new document object that contains the same data as the original document. * @throws TransformerException Thrown if the document can't be */ public static Document cloneDocument(final Document doc) throws TransformerException { final Node rootNode = doc.getDocumentElement(); // Copy the doctype and xml version type data final TransformerFactory tfactory = TransformerFactory.newInstance(); final Transformer tx = tfactory.newTransformer(); final DOMSource source = new DOMSource(doc); final DOMResult result = new DOMResult(); tx.transform(source, result); // Copy the actual content into the new document final Document copy = (Document) result.getNode(); copy.removeChild(copy.getDocumentElement()); final Node copyRootNode = copy.importNode(rootNode, true); copy.appendChild(copyRootNode); return copy; }
From source file:Main.java
public static void appendChild(Node parent, Node... nodes) { Document doc = parent.getOwnerDocument(); for (Node node : nodes) { if (node.getOwnerDocument() != doc) { parent.appendChild(doc.importNode(node, true)); } else {//from w w w. j av a 2s .c o m parent.appendChild(node); } } }
From source file:Main.java
/** * Print out the node in XML format/*from ww w . ja v a 2s. co m*/ * * @param node * @param out */ public static void printNode(Node node, PrintStream out) { Document doc = new DocumentImpl(); Element topLevel = doc.createElement("xml"); doc.appendChild(topLevel); topLevel.appendChild(doc.importNode(node, true)); OutputFormat format = new OutputFormat(doc); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); XMLSerializer serializer = new XMLSerializer(out, format); try { serializer.serialize(doc); } catch (IOException ioException) { ioException.printStackTrace(); } }
From source file:de.betterform.cache.CacheManager.java
/** * * @param file cache key/* w ww.ja va 2 s . co m*/ * @return document cache value * @throws XFormsException */ public static Document getDocument(File file) throws XFormsException { Element elem = getElementFromFileCache(file); if (elem == null) { return null; } Document cachedDoc = (Document) elem.getObjectValue(); org.w3c.dom.Element elment = cachedDoc.getDocumentElement(); Document document = DOMResource.newDocument(); //document.appendChild(document.adoptNode(elment.cloneNode(true))); document.appendChild(document.importNode(elment, true)); return document; }
From source file:Main.java
/** * @param parent//from ww w. j av a 2 s . co m * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder docBuilder = domFactory.newDocumentBuilder(); Document doc = parent.getOwnerDocument(); Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement(); fragmentNode = doc.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
From source file:cz.muni.fi.mir.mathmlunificator.utils.DOMBuilder.java
/** * Create new W3C DOM document with only child node created as the clone of * the given node (detached from the DOM of the source node). * * @param nodeToClone Node to clone as the only child node of the new * document.//from ww w . j av a 2 s . com * @param deep If true, recursively clone the subtree under the given node; * if false, clone only the node itself (and its attributes, if it is an * Element). * @return New W3C DOM document with the only child node created as the * clone of the given node (detached from the DOM of the source node). */ public static Document createNewDocWithNodeClone(Node nodeToClone, boolean deep) { Document newDoc = DOMBuilder.createEmptyDoc(); newDoc.appendChild(newDoc.importNode(nodeToClone, deep)); return newDoc; }
From source file:Main.java
/** * @param parent//from w w w . j a v a 2s .co m * node to add fragment to * @param fragment * a well formed XML fragment * @throws ParserConfigurationException */ public static void appendXmlFragment(Node parent, String fragment) throws IOException, SAXException, ParserConfigurationException { DocumentBuilder docBuilder = getBuilder(); Document doc = parent.getOwnerDocument(); Node fragmentNode = docBuilder.parse(new InputSource(new StringReader(fragment))).getDocumentElement(); fragmentNode = doc.importNode(fragmentNode, true); parent.appendChild(fragmentNode); }
From source file:Main.java
/** * Insert a node into the document.//w ww . java2s.c o m * * @param poDocument Source Document XML * @param poNode Node where the new node is going to be inserted into * @param poNodeToInsert Node to be inserted into * @param pbFlag Flag to indicate if poNodeToInsert is created by document poDocument * * @return Node */ public static void insertNode(Document poDocument, Node poNode, Node poNodeToInsert, boolean pbFlag) { Node elementNode = null; try { if (!pbFlag) { elementNode = poDocument.importNode(poNodeToInsert, true); poNode.appendChild(elementNode); } else { poNode.appendChild(poNodeToInsert); } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
/** * convertToSoapElement//from w w w.j a v a2 s .c o m * * @param doc the Owner Soap Document of the SOAPElement to be returned * @param elem the DOM Element to be converted to SOAPElement * @return a SOAPElement whose parent node is null * @throws DOMException */ public static SOAPElement convertToSoapElement(Document doc, Element elem) throws DOMException, ClassCastException { if (elem instanceof SOAPElement) return (SOAPElement) elem; return (SOAPElement) doc.importNode(elem, true); }
From source file:Main.java
public static void edit(Document doc) { Element element = doc.getDocumentElement(); Element element2 = doc.createElement("newname"); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); }/*from w w w . jav a 2 s .co m*/ while (element.hasChildNodes()) { element2.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(element2, element); }