title: Add root element to XML in Java
body: <p>I hava an XML String that looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;id&gt;500404&lt;/id&gt;
  &lt;parentid&gt;0&lt;/parentid&gt;
  &lt;version&gt;1&lt;/version&gt;
  &lt;nodes&gt;
      &lt;id&gt;1015252&lt;/id&gt;
      &lt;category&gt;NODE&lt;/category&gt;
      &lt;modified&gt;false&lt;/modified&gt;
      &lt;X&gt;80&lt;/X&gt;
      &lt;Y&gt;30&lt;/Y&gt;
  &lt;/nodes&gt;
  </code></pre>
  
  <p>I want to programatically modify it by adding a root node tag called "model" .
  I tried to parse the string using DOM, read all child nodes and then to create a new xml doc of type <code>Document</code> with a root node called "model" which has as child nodes all the nodes of the XML String I allready have. 
  But it is giving me the following exception :
  <strong>The markup in the document following the root element must be well-formed.</strong></p>
  
  <p>My code so far is :</p>
  
  <pre><code>String xmlString = "XML GOES HERE"
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          InputSource inputstream = new InputSource(new StringReader(
                  xmlString));
          Document doc = builder.parse(inputstream);
          NodeList nodes = doc.getChildNodes();
                  Document doc2 = docBuilder.newDocument();
          Element rootElement = doc2.createElement("model");
          doc2.appendChild(rootElement);
                  for (int k = 0; k &lt; nodes.getLength(); k++)
              {
                  Node childn = nodes.item(k);
                  rootElement.appendChild(childn);
                           }
  </code></pre>
  
  <p>Any help please ? </p>