Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know you got the issue solved already, but I still wanted to take a stab at this problem using the <a href="http://xom.nu/" rel="nofollow noreferrer">XOM</a> library that I'm currently testing out (related to <a href="https://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j">this question</a>), and while doing that, offer a different approach than that of Andreas_D's answer.</p> <p>(To simplify this example, I put your <code>&lt;alert-set&gt;</code> and <code>&lt;weather-set&gt;</code> into separate files, which I read into <a href="http://xom.nu/apidocs/nu/xom/Document.html" rel="nofollow noreferrer"><code>nu.xom.Document</code></a> instances.)</p> <pre><code>import nu.xom.*; [...] Builder builder = new Builder(); Document alertDoc = builder.build(new File("src/xomtest", "alertset.xml")); Document weatherDoc = builder.build(new File("src/xomtest", "weatherset.xml")); Document mainDoc = builder.build("&lt;DataSet&gt;&lt;blank/&gt;&lt;blank/&gt;&lt;/DataSet&gt;", ""); Element root = mainDoc.getRootElement(); root.replaceChild( root.getFirstChildElement("blank"), alertDoc.getRootElement().copy()); root.replaceChild( root.getFirstChildElement("blank"), weatherDoc.getRootElement().copy()); </code></pre> <p>The key is to make a copy of the elements to be inserted into <code>mainDoc</code>; otherwise you'll get a complain that "child already has a parent".</p> <p>Outputting mainDoc now gives:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;DataSet&gt; &lt;alert-set&gt; &lt;warning&gt;National Weather Service...&lt;/warning&gt; &lt;start-date&gt;5/19/2009&lt;/start-date&gt; &lt;end-date&gt;5/19/2009&lt;/end-date&gt; &lt;/alert-set&gt; &lt;weather-set&gt; &lt;chance-of-rain type="percent"&gt;31&lt;/chance-of-rain&gt; &lt;conditions&gt;Partly Cloudy&lt;/conditions&gt; &lt;temperature type="Fahrenheit"&gt;78&lt;/temperature&gt; &lt;/weather-set&gt; &lt;/DataSet&gt; </code></pre> <p>To my delight, this turned out to be very straight-forward to do with XOM. It only took a few minutes to write this, even though I'm definitely <em>not</em> very experienced with the library yet. (It would have been even easier without the <code>&lt;blank/&gt;</code> elements, i.e., starting with simply <code>&lt;DataSet&gt;&lt;/DataSet&gt;</code>.)</p> <p>So, unless you have compelling reasons for using only the standard JDK tools, I warmly recommend trying out XOM as it can make XML handling in Java much more pleasant.</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.
 

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