title: Insert XML structure as a child of another XML Element using PHP
body: <p>Is it possible to insert a PHP <code>SimpleXMLElement</code> as a child of another Element</p>
  
  <pre><code>$xml   = new SimpleXMLElement('&lt;root/&gt;');
  $xml_a = new SimpleXMLElement('&lt;parent/&gt;');
  $xml_b = new SimpleXMLElement('&lt;child/&gt;');
  $xml_b-&gt;addAttribute('attribute','value');
  $xml_b-&gt;addChild('item','itemValue');
  // the part will cause and error
  $xml_a-&gt;addChild($xml_b);
  $xml-&gt;addChild($xml_a);
  </code></pre>
  
  <p>I know the above code doesn't work, but that would be the sort of thing I'm looking for.</p>
  
  <p>So that the structure looks like:</p>
  
  <pre><code>&lt;root&gt;
   &lt;partent&gt;
    &lt;child attribute="value"&gt;
     &lt;item&gt;itemValue&lt;/item&gt;
    &lt;/child&gt;
   &lt;/parent&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I have tried using something like the below to <code>addChild()</code>  :</p>
  
  <pre><code>$dom = dom_import_simplexml($xml_a);
  $_xml_ = $dom-&gt;ownerDocument-&gt;saveXML($dom-&gt;ownerDocument-&gt;documentElement);
  $xml-&gt;addChild('parent',$_xml_);
  </code></pre>
  
  <p>The solution im sure is relatively simple, but I've not used <code>SimpleXMLElement</code> like this before.</p>