Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For what it's worth, here's a solution I came up with using the <a href="http://dom4j.sourceforge.net/" rel="nofollow noreferrer"><strong>dom4j</strong></a> library. (I did check that it works.)</p> <p>Read the XML fragment into a <code>org.dom4j.Document</code> (note: all the XML classes used below are from org.dom4j; see Appendix): </p> <pre><code> String newNode = "&lt;node&gt;value&lt;/node&gt;"; // Convert this to XML SAXReader reader = new SAXReader(); Document newNodeDocument = reader.read(new StringReader(newNode)); </code></pre> <p>Then get the Document into which the new node is inserted, and the parent Element (to be) from it. (Your org.w3c.dom.Document would need to be converted to org.dom4j.Document here.) For testing purposes, I created one like this:</p> <pre><code> Document originalDoc = new SAXReader().read(new StringReader("&lt;root&gt;&lt;given&gt;&lt;/given&gt;&lt;/root&gt;")); Element givenNode = originalDoc.getRootElement().element("given"); </code></pre> <p>Adding the new child element is very simple:</p> <pre><code> givenNode.add(newNodeDocument.getRootElement()); </code></pre> <p>Done. Outputting <code>originalDoc</code> now yields: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;root&gt; &lt;given&gt; &lt;node&gt;value&lt;/node&gt; &lt;/given&gt; &lt;/root&gt; </code></pre> <p><strong>Appendix</strong>: Because your question talks about <code>org.w3c.dom.Document</code>, here's how to convert between that and <code>org.dom4j.Document</code>.</p> <pre><code>// dom4j -&gt; w3c DOMWriter writer = new DOMWriter(); org.w3c.dom.Document w3cDoc = writer.write(dom4jDoc); // w3c -&gt; dom4j DOMReader reader = new DOMReader(); Document dom4jDoc = reader.read(w3cDoc); </code></pre> <p>(If you'd need both kind of <code>Document</code>s regularly, it might make sense to put these in neat utility methods, maybe in a class called <code>XMLUtils</code> or something like that.)</p> <p>Maybe there are better ways to do this, even without any 3rd party libraries. But out of the solutions presented so far, in my view this is the easiest way, even if you need to do the dom4j &lt;-> w3c conversions.</p> <p><strong>Update</strong> (2011): before adding dom4j dependency to your code, note that <a href="https://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j">it is <em>not</em> an actively maintained project, and has some other problems too</a>. Improved version 2.0 has been in the works for ages, but there's only an alpha version available. You may want to consider an alternative, like XOM, instead; read more in the question linked above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload