title: How do I extract child element from XML to a string in Java?
body: <p>If I have an XML document like</p>
  
  <pre><code>&lt;root&gt;   
     &lt;element1&gt;
          &lt;child attr1="blah"&gt;
             &lt;child2&gt;blahblah&lt;/child2&gt;
          &lt;child&gt;   
     &lt;/element1&gt; 
  &lt;/root&gt;
  </code></pre>
  
  <p>I want to get an XML string with the first child element.  My output string would be  </p>
  
  <pre><code>&lt;element1&gt;
      &lt;child attr1="blah"&gt;
         &lt;child2&gt;blahblah&lt;/child2&gt;
      &lt;child&gt;
  &lt;/element1&gt;
  </code></pre>
  
  <p>There are many approaches, would like to see some ideas.  I've been trying to use Java XML APIs for it, but it's not clear that there is a good way to do this.</p>
  
  <p>thanks</p>