title: Writing xml tags with SimpleXml
body: <p>I have this xml file: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;products&gt;
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>And i wrote this php code:</p>
  
  <pre><code>&lt;?php
  ....
  ....
  $xml=simplexml_load_file("myfile.xml") or die("Error: Cannot create object");
  
  foreach ($xml-&gt;xpath("/products/product") as $prod) 
  {
      $xmlname = $xml-&gt;addChild("name",$name);
      $xmlid = $xml-&gt;addChild("id",$id);
      $xmlprice = $xml-&gt;addChild("price",$price);
  }
  $xml-&gt;asXML ("result_file.xml");
  ?&gt;
  </code></pre>
  
  <p>This works, but not as I want. It creates the file 'result_file.xml' with new nodes: $name,$id and $price. The problem is that all these nodes are inserted at the end of the xml file like this:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;products&gt;
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    ... some nodes 
  &lt;/product&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>But I'd like to insert all these nodes after their product node.
  For example, i want to obtain:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;products&gt;
  &lt;product&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
    ... some nodes 
  &lt;/product&gt;
  
  &lt;product&gt;
    &lt;name&gt;$name&lt;/name&gt;
    &lt;id&gt;$id&lt;/id&gt;
    &lt;price&gt;$price&lt;/price&gt;
    ... some nodes 
  &lt;/product&gt;
  &lt;/products&gt;
  </code></pre>