List of usage examples for org.w3c.dom DocumentFragment appendChild
public Node appendChild(Node newChild) throws DOMException;
newChild
to the end of the list of children of this node. From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*www . j ava 2 s . com*/ Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); String fragment = "<fragment>aaa</fragment>"; factory = DocumentBuilderFactory.newInstance(); Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment))); Node node = doc.importNode(d.getDocumentElement(), true); DocumentFragment docfrag = doc.createDocumentFragment(); while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); } Element element = doc.getDocumentElement(); element.appendChild(docfrag); }
From source file:Main.java
public static void addFragment(Document doc) { Element person;//w w w.j a v a 2s . c o m Element root = doc.getDocumentElement(); DocumentFragment fragment = doc.createDocumentFragment(); person = makePersonNode(doc, "Name 1", "555-4444"); fragment.appendChild(person); person = makePersonNode(doc, "Name 2", "555-9999"); fragment.appendChild(person); root.appendChild(fragment); }
From source file:Main.java
/** * Tranform a node list into a document fragment */// w ww . j av a2s . c om public static Node toDocumentFragment(NodeList list) { if (list.getLength() == 1 && list.item(0) instanceof Document) return list.item(0); Document document = newDocument(); DocumentFragment fragment = document.createDocumentFragment(); for (int i = 0; i < list.getLength(); i++) fragment.appendChild(document.adoptNode(list.item(i).cloneNode(true))); return fragment; }
From source file:Main.java
/** * Will create a documentFragment of the replacingDocument, will import the * replacingDocument as a node of the replacedDocument, and then will * replace the replaceNode with the documentFragment of replacingDocument. * //from w ww . j a v a2 s . com * @param replacedDocument * The document which will have a node replace * @param replacingDocument * The document that will replace a node * @param replacedNode * The node in replacedDocument that will be replaced * @return The new version of replacedDocument will replacedNode replaced */ public static Node replaceNode(Document replacedDocument, Document replacingDocument, Node replacedNode) { // Create a documentFragment of the replacingDocument DocumentFragment docFrag = replacingDocument.createDocumentFragment(); Element rootElement = replacingDocument.getDocumentElement(); docFrag.appendChild(rootElement); // Import docFrag under the ownership of replacedDocument Node replacingNode = ((replacedDocument).importNode(docFrag, true)); // In order to replace the node need to retrieve replacedNode's parent Node replaceNodeParent = replacedNode.getParentNode(); replaceNodeParent.replaceChild(replacingNode, replacedNode); return replacedDocument; }
From source file:Main.java
/** * Will create a documentFragment of the replacingDocument, will import the * replacingDocument as a node of the replacedDocument, and then will * replace the replaceNode with the documentFragment of replacingDocument. * /* w w w .j av a 2s . c o m*/ * @param replacedDocument * The document which will have a node replace * @param replacingDocument * The document that will replace a node * @param replacedNode * The node in replacedDocument that will be replaced * @return The new version of replacedDocument will replacedNode replaced */ public static Node replaceNode(final Document replacedDocument, final Document replacingDocument, final Node replacedNode) { // Create a documentFragment of the replacingDocument DocumentFragment docFrag = replacingDocument.createDocumentFragment(); Element rootElement = replacingDocument.getDocumentElement(); docFrag.appendChild(rootElement); // Import docFrag under the ownership of replacedDocument Node replacingNode = ((replacedDocument).importNode(docFrag, true)); // In order to replace the node need to retrieve replacedNode's parent Node replaceNodeParent = replacedNode.getParentNode(); replaceNodeParent.replaceChild(replacingNode, replacedNode); return replacedDocument; }
From source file:DOMEdit.java
public static void addFragment(Document doc) { Element person;/*w ww.ja v a2 s .c om*/ Element root = doc.getDocumentElement(); DocumentFragment fragment = doc.createDocumentFragment(); person = makePersonNode(doc, "Name 1", "555-4444"); fragment.appendChild(person); person = makePersonNode(doc, "Name 2", "555-9999"); fragment.appendChild(person); root.appendChild(fragment); }
From source file:com.portfolio.data.utils.DomUtils.java
private static void insererXML(Connection connexion, Document xmlSourceDoc, String partageableId, StringBuffer outTrace, boolean trace) throws Exception { // --------------------------------------------------- if (trace)/* ww w . j av a 2 s . c o m*/ outTrace.append("<br>insererPartageable -- entre"); if (trace) outTrace.append("<br>partageableId=" + partageableId); // ===============chargement du document source ======================================== if (trace) outTrace.append("<br>lecture du document xml :" + partageableId + "..."); Document xmlPartageable = buildDOM(readXmlString(connexion, partageableId, outTrace)); if (trace) outTrace.append(" ok"); DocumentFragment aInserer = xmlSourceDoc.createDocumentFragment(); NodeList liste = xmlPartageable.getDocumentElement().getChildNodes(); int nbListe = liste.getLength(); if (trace) outTrace.append("<br> nbListe=" + nbListe); for (int i = 0; i < nbListe; i++) { aInserer.appendChild(xmlSourceDoc.importNode(liste.item(i), true)); } xmlSourceDoc.getFirstChild().insertBefore(aInserer, xmlSourceDoc.getFirstChild().getFirstChild()); if (trace) outTrace.append("<br>insererPartageable -- sortie"); }
From source file:XMLBody.java
private String serializeNodes(NodeList childNodes) throws Exception { DocumentFragment fragment = xmlDocument.createDocumentFragment(); for (int i = 0; i < childNodes.getLength(); i++) { fragment.appendChild(childNodes.item(i).cloneNode(true)); }//from w ww . jav a 2 s . c om try { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); StringWriter out = new StringWriter(); StreamResult result = new StreamResult(out); transformer.transform(new DOMSource(fragment), result); return out.toString(); } catch (Exception e) { throw new Exception(e); } }
From source file:org.callimachusproject.server.RoundTripTest.java
public void testSingleDocumentFragment() throws Exception { DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance(); Document doc = builder.newDocumentBuilder().newDocument(); DocumentFragment frag = doc.createDocumentFragment(); frag.appendChild(doc.createElement("root")); trip.documentFragment(frag);/*from w w w . j ava 2 s. c o m*/ assertEquals(null, trip.documentFragment(null)); }
From source file:org.callimachusproject.server.RoundTripTest.java
public void testSetOfSingleDocumentFragment() throws Exception { DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance(); Document doc = builder.newDocumentBuilder().newDocument(); DocumentFragment frag = doc.createDocumentFragment(); frag.appendChild(doc.createElement("root")); trip.setOfDocumentFragment(singleton(frag)); assertEquals(EMPTY_SET, trip.setOfDocumentFragment(EMPTY_SET)); }