title: Inject XML into a node using a string
body: <p>I am rebuilding a nodes children by saving them out to an array as strings, trashing them in the XML, inserting a new child node into the array as a string...  now I want to loop through the array and write them back out to the original node.  The problem is I can't find anything on how to add a child node using a string.</p>
  
  <p>See below for my code. Thanks!!!</p>
  
  <pre><code>$xml = simplexml_load_file($url);
  
      $questionGroup = $xml-&gt;qa[intval($id)];
  
      $children = array(); //  create empty array
  
      foreach ($questionGroup-&gt;children() as $element) {  //  loop thru children
          array_push($children, $element-&gt;asXML()); // save XML into array
      }
      //unset($questionGroup-&gt;answer);
      //unset($questionGroup-&gt;question);
  
      //create new node
      $newNode = '&lt;answer&gt;&lt;title&gt;'.$title.'&lt;/title&gt;&lt;description&gt;'.$description.'&lt;/description&gt;&lt;subName&gt;'.$subName.'&lt;/subName&gt;&lt;date&gt;'.$date.'&lt;/date&gt;&lt;timestamp&gt;'.$timestamp.'&lt;/timestamp&gt;&lt;/answer&gt;';
  
      echo "children count: ".count($children);
      echo "&lt;br /&gt;&lt;br /&gt;";
      print_r($children);
      echo "&lt;br /&gt;&lt;br /&gt;";
      // insert new
      array_splice($children,intval($elementIndex),0,$newNode);
      echo "children count: ".count($children);
      echo "&lt;br /&gt;&lt;br /&gt;";
      print_r($children);
      echo "&lt;br /&gt;&lt;br /&gt;";
      echo $questionGroup-&gt;asXML();
      foreach ($children as $element) {  //  loop thru array 
          echo "&lt;br /&gt;&lt;br /&gt;";
          echo $element;
          //$questionGroup-&gt;addChild(simplexml_load_string($element));  //  add array element to the empty questionGroup
  
      } 
          echo "&lt;br /&gt;&lt;br /&gt;";
      echo "questionGroup: ".$questionGroup;
  </code></pre>
  
  <p>UPDATE:
  I found a function that I modified and was able to get working:</p>
  
  <pre><code>function append_simplexml(&amp;$simplexml_to, &amp;$simplexml_from)
  {
     $childNode = $simplexml_to-&gt;addChild($simplexml_from-&gt;getName(), "");
      foreach ($simplexml_from-&gt;children() as $simplexml_child)
      {
         $simplexml_temp = $childNode-&gt;addChild($simplexml_child-&gt;getName(), (string) $simplexml_child);
         foreach ($simplexml_child-&gt;attributes() as $attr_key =&gt; $attr_value)
         {
            $simplexml_temp-&gt;addAttribute($attr_key, $attr_value);
         }
  
        // append_simplexml($simplexml_temp, $simplexml_child);
      }
  } 
  </code></pre>
  
  <p>With this usage in my foreach() loop:</p>
  
  <pre><code>foreach ($children as $element) {  //  loop thru array 
      append_simplexml($questionGroup, new SimpleXMLElement($element));
  }
  </code></pre>