title: capture data from XML and save in database
body: <p>I am working on XML file, which i need to read through PHP and then save that data in a database. So far i was successful. The problem is that, it is made specifically for the XML below. What i need, is a generic code which could do the same but for any kind of XML code. </p>
  
  <p>Here is my XML and PHP code </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  
  &lt;list&gt;
  
  &lt;person&gt;
  
        &lt;person_id&gt;1&lt;/person_id&gt;
        &lt;fname&gt;Mikael&lt;/fname&gt;
        &lt;lname&gt;Ronstrom&lt;/lname&gt;
    &lt;/person&gt;
    &lt;person&gt;
        &lt;person_id&gt;2&lt;/person_id&gt;
        &lt;fname&gt;Lars&lt;/fname&gt;
        &lt;lname&gt;Thalmann&lt;/lname&gt;
    &lt;/person&gt;
     &lt;person&gt;
        &lt;person_id&gt;3&lt;/person_id&gt;
        &lt;fname&gt;Mikael&lt;/fname&gt;
        &lt;lname&gt;Ronstrom&lt;/lname&gt;
    &lt;/person&gt;
    &lt;person&gt;
        &lt;person_id&gt;4&lt;/person_id&gt;
        &lt;fname&gt;Lars&lt;/fname&gt;
        &lt;lname&gt;Thalmann&lt;/lname&gt;
    &lt;/person&gt;
     &lt;person&gt;
        &lt;person_id&gt;5&lt;/person_id&gt;
        &lt;fname&gt;Mikael&lt;/fname&gt;
        &lt;lname&gt;Ronstrom&lt;/lname&gt;
    &lt;/person&gt;
    &lt;person&gt;
        &lt;person_id&gt;6&lt;/person_id&gt;
        &lt;fname&gt;Lars&lt;/fname&gt;
        &lt;lname&gt;Thalmann&lt;/lname&gt;
  
    &lt;/person&gt;
   &lt;/list&gt; 
  </code></pre>
  
  <p>here is my Php code</p>
  
  <pre><code>$con = mysql_connect("localhost","root","vertrigo");
  if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("test", $con);
  
  
  
  $xml = simplexml_load_file("xml.xml");
  
  
  $aa = "";
  foreach($xml-&gt;children() as $child)
  {
  
  
  foreach($child-&gt;children() as $childs)
  {
   $aa .= "'".$childs ."',";
  
  
  }
  $aa = substr_replace($aa, "", -1);
  
  $sql = "INSERT into tbl_xmldata (person_id,first_name,last_name) values ( $aa )";
  $aa = "";
  $rr = mysql_query($sql);
  
  } 
  if ($rr){ echo "data successfully captured from XML and inserted in db";}else{ echo "sorry no data insertion";}
  </code></pre>