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

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

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

Post by impulse() »

Just a quick script to get me started on parsing XML data into a PHP variable.

Regards,
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

What about simplexml_load_file()?
And what is SimpleXMLElement d11wtq? I don't see it in the manual :?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Use foreach(). That's what its there for.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Have you ever parsed XML using

Code: Select all

xml_set_element_handler
before?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Yes, why?
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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.
Post Reply