Page 1 of 1

Would someone draw up a Q&D example of parsing XML into

Posted: Fri Oct 13, 2006 4:57 am
by impulse()
Just a quick script to get me started on parsing XML data into a PHP variable.

Regards,

Posted: Fri Oct 13, 2006 5:12 am
by Chris Corbyn
Have you read this? http://uk.php.net/simplexml

Code: Select all

$xml = new SimpleXMLElement(file_get_contents('/path/to/xml/file.xml'));
echo $xml->someTag;

Posted: Fri Oct 13, 2006 5:22 am
by Oren
What about simplexml_load_file()?
And what is SimpleXMLElement d11wtq? I don't see it in the manual :?

Posted: Fri Oct 13, 2006 5:42 am
by impulse()
OK. Now I am able to put certain bits of the XML data into variables using:

Code: Select all

$newVar = $xml->stephen->age;
My plan is to insert all data from an XML file into a MySQL DB. How would it be possible to scan the $xml array and insert what data is found into a MySQL DB. I know the part of inserting into a DB, I'm just a bit lost on how you would extract the data from an XML document to be able to insert it.

Posted: Fri Oct 13, 2006 6:18 am
by Chris Corbyn
Oren wrote:And what is SimpleXMLElement d11wtq? I don't see it in the manual :?
I see it in the Manual :? http://uk.php.net/simplexml I've never used it before, but it's on that page when you read the code examples.

Posted: Fri Oct 13, 2006 6:25 am
by impulse()
I have this code, which works, to a certain degree.

Code: Select all

$xml = simplexml_load_file('data.xml');

while (list($k) = each ($xml)) {
  echo $k;
  }
But this creates an infinite loops of the first name in my XML file. If I put

Code: Select all

break;
after the echo it only prints the first tags data.

Posted: Fri Oct 13, 2006 6:27 am
by Chris Corbyn
Use foreach(). That's what its there for.

Posted: Fri Oct 13, 2006 6:31 am
by impulse()
I have already tried with

Code: Select all

foreach ($xml->stephen->age as $value) {
  echo $value;
But I can only print the data held within the <stephen> tags.

I'm unsure of how to get it to scan through the XML and print out

Code: Select all

$xml->ALL->ALL
"ALL" being everything.

Posted: Fri Oct 13, 2006 6:56 am
by impulse()
This is sorted now. The error was in my XML, not my PHP.

Code: Select all

$xml = simplexml_load_file('data.xml');

foreach ($xml->friend as $friend) {
  printf("Name: %s <br> ", $friend->name);
  printf("Age: %s <br> ", $friend->age);
  }
Thank you

Posted: Fri Oct 13, 2006 10:24 am
by Oren
d11wtq wrote:I see it in the Manual :? http://uk.php.net/simplexml I've never used it before, but it's on that page when you read the code examples.
Oh yeah, I see it too now. I simply searched in the Function List (http://us2.php.net/manual-lookup.php?pa ... XMLElement) and couldn't find it :P

Posted: Fri Oct 13, 2006 10:46 am
by impulse()
Have you ever parsed XML using

Code: Select all

xml_set_element_handler
before?

Posted: Fri Oct 13, 2006 1:16 pm
by volka
Yes, why?

Posted: Sat Oct 14, 2006 5:55 am
by impulse()
Could you give me a little help on how you run through an XML file and extract each tag into a MySQL DB. If I have 3 pieces of information inside a tag. For example

Code: Select all

<friends>
  <friend>
    <name> Stephen </name>
    <age> 21 </name>
    <sex> Male </name>
  </friend>

  <friend>
    <name> Scott </name>
    <age> 20 </age>
    <sex> Male </age>
  </friend>

  <friend>
    <name> Helena </name>
    <age> 20 </age>
    <sex> Female </sex>
  </friend>
</friends>
And I have a MySQL DB with the colums: ID, Name, Age, Sex. With these I want to insert the name, age & sex of each person onto a row in the DB.
For doing so I have to use

Code: Select all

xml_set_character_data_handler
because of its extended functionality compared to

Code: Select all

simplexml_load_file
I have thought of a couple of ways to do so. I'm currently in the process of doing the following:

For the 3 arguments for xml_set_character_data_handler (start tag, tag contents & end tag) I will leave the start and end tag functions empty and then in the tag contents function I will have the following code:

Code: Select all

function tag_contents($parser, $data) {
if ($j =<4) {
             switch ($i) {
               case 0:
                 mysql_query("INSERT INTO test (name) VALUES ('$data')");
                 $i++;
                 $j++;
                 break;
               case 1:
                 mysql_query("INSERT INTO test (age) VALUES ('$data')");
                 $i++;
                 $j++
                 break;
               case 2:
                 mysql_query("INSERT INTO test (sex) VALUES ('$data')");
                 $i++;
                 $j++;
                 break;
                }
else $j = 0;
           }
         }


I know this doesn't work and it's just to show you an idea I had. I know you'll understand me easier via code than English.

Hope you can help, Stephen.