Forum Moderators: coopster

Message Too Old, No Replies

SimpleXML

Question about SimpleXML

         

CWebguy

12:22 am on Sep 11, 2007 (gmt 0)

10+ Year Member



Hello, I am new to PHP and MySQL and trying to learn, but can somebody help me to store XML data into a database (MySQL) using PHP (and the example XML file at "http://php.net/simplexml"?)

Thanks!

[edited by: CWebguy at 12:28 am (utc) on Sep. 11, 2007]

Habtom

5:12 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To store the values in the data actually is easy.

See the first example there on the link you provided:


<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie[0]->plot; // "So this language. It's like..."
?>

To access the plot section of the xml, you use:

$xml->movie[0]->plot;

To keep it simple, find out the values you need to store in the db in this fashion, and use the insert statement to add them to the db.

Unless I completely missed your point, this can be handled simply.

Habtom

CWebguy

4:56 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



Habtom, thank you so much for the response and your time to help! Yes, what I am really looking for is an understanding on all this. I'm going to try to work through this here based on what you replied.

<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie[0]->plot; // "So this language. It's like..."
?>

Ok, this first example looks to me like it calls the XML file using the built in PHP SimpleXML function. Then the next part prints out the XML data.

I'm guessing that SimpleXML stores each XML tag as a new array? (This is the part that I have been unsure of). So where it says <plot>So, this language. It's like...</plot> in the XML, it is now a PHP array with the name "plot" (or maybe $plot?)?

So now to enter the data into MySQL database for the subject "plot", would I use the code:

mysql_query ("INSERT INTO "name of table" (plotdata) VALUES ('plot')"); //or ('$plot') if it's stored as $plot

Is this right or am I missing something? Thank you so much for all your help as I try to learn! :) God bless!

[edited by: CWebguy at 4:59 pm (utc) on Sep. 11, 2007]

Habtom

5:46 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would assume $plot to be as follows:

$plot = $xml->movie[0]->plot;

And you can add this now.

You might need to make a few tests, and see how that xml on the example give is similar to the one you have.

CWebguy

7:16 pm on Sep 11, 2007 (gmt 0)

10+ Year Member



Awesome! Thanks so much so far Habtom! You are a big help! I will definitly give this a try and let you know what I come up with!

Also, if I will be using this to import data from XML to MySQL, should I create a page for this sole purpose alone with nothing but the code we just used to import the XML and just run it when I need it? (I'm not sure yet if I'll be doing it on a regular basis or not, but possibly maybe like weekly or something along those lines. But at most I will need to run it once to populate database).

Thanks!

[edited by: CWebguy at 7:17 pm (utc) on Sep. 11, 2007]