0

How to populate MySQL DB from $xml defined as follows?

$xml = simplexml_load_string($line);

Is there any predefine function that automates some parts of this job? Or do I need to use DOM?

1

3 Answers 3

2

I think this should work. I have used the following code, and worked well for me. This code not only adds data for one xml file but can be used for multiple xml files.

    <?php

        // table headers --> SNo  Field  Value 

        $connection = mysql_connect("localhost","root","password"); 
        if (!$connection) {
            die("Database connection failed: " . mysql_error());
        }

        $db_select = mysql_select_db("forms_data",$connection);
        if (!$db_select) {
            die("Database selection failed: " . mysql_error());
        }

        mysql_query("CREATE table form_try11(id int(11) not null auto_increment,
                     Field varchar(100), 
                     Value varchar(100),
                     primary key(id)
                    )");


       $file1="C:\webserver\Apache\htdocs\php_json\file1.xml";
       $file2="C:\webserver\Apache\htdocs\php_json\file2.xml";
       $file3="C:\webserver\Apache\htdocs\php_json\file3.xml";

      $path_arr = Array($file1,$file2,$file3);

      for($count1=0; $count1<count($path_arr); $count1++){
      $xml = simplexml_load_file($path_arr[$count1]);
      $string= json_encode($xml);     

      $jsonIterator = new RecursiveIteratorIterator(
            new RecursiveArrayIterator(json_decode($string, TRUE)),
            RecursiveIteratorIterator::SELF_FIRST);

          foreach ($jsonIterator as $key => $val) {
                if(is_array($val) || $key=="xmlns:xfa" || $key=="xfa:dataNode") {

                    continue;
                } 
                else {

                        $db_insert= mysql_query("INSERT INTO form_try11(Field,Value) VALUES('$key','$val') ");

                        if (!$db_insert) {
                            die("Database insert failed: " . mysql_error());
                        } 
                   }  
            }  



    }


    mysql_close($connection);

    ?>
Sign up to request clarification or add additional context in comments.

Comments

1

No predefined functions that I know of, but you can do the following:

See all the variables using

var_dump($xml);

And then do a loop to insert them into the database like this:

foreach($xml as $element)
{
    $PDO->prepare('INSERT .....');
    $PDO->bindParam(blah,blah);
    $PDO->execute()
}

4 Comments

Tha problem is that var_dump($xml) returns object(SimpleXMLElement)#1685 (0) { }, while var_dump($line) returns a list of numeric values (as it was expected). so, how can I know the names of parameters?
Is the data in $line valid XML? I'm not sure why it should give you an error. You can do the names of the parameters like this $line['param1'] and so on
var_dump doesn't work well with SimpleXML objects. Your XML node is probably fine just var_dump doesn't see it
@GusGus surrond your var dump with <pre>. echo '<pre>'; var_dump(xml); echo '</pre>';
1

If you are trying to pull all file to a table, You can import it using SQL statements.

LOAD XML LOCAL INFILE 'your_xml_file.xml' 
INTO TABLE your_table_name(field_one,field_two,....);

See Load XML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.