954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP take XML data

Hi There,

I have an XML data

<coordinators branch="xx"><coordinator cnum="xx">
<value key="role" type="string">BC</value>
<value key="role_desc" type="string">role</value>
<value key="name" type="string">xx</value>
<value key="tel_number" type="string"/>
<value key="email_address" type="string">xx</value>
</coordinator>
<coordinator cnum="xx">
<value key="role" type="string">BC</value>
<value key="role_desc" type="string">role</value>
<value key="name" type="string">xx</value>
<value key="tel_number" type="string"/>
<value key="email_address" type="string">xx</value>
</coordinator>
</coordinators>


I need to get name and email address from this like $names $emails ...

Any one have an idea?

Thanks

kadriirdak
Light Poster
33 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thx for that.

Any coding example maybe?

I did search online but couldnt sort it out..

kadriirdak
Light Poster
33 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

See example #5 in the documentation I linked.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi,

This maybe the simplest example I could ever think of.. Say, your above xml file is called test.xml, then you can easily parse the email XX and the name: XX from it by using these few lines of php codes.

<?php
## type in your xml file location
$xml ='test.xml';
## remove @ to debug
$xml = @simplexml_load_file($xml); 
 foreach($xml as $items)
{
echo "Name: ".$items->value[2]." Email: ".$items->value[4]."<br/>";

}

  ?>


We can also assign them into variables as you wish, as shown in my example below..

$name = $items->value[2];
$email = $items->value[4];


You may want to consider setting up some check points to see if the xml file does exist, like so

if (file_exists('test.xml')) {
$xml = @simplexml_load_file($xml);

}
## instruct the script what to do if xml file does not exist.

else{

## it's your call here

}

veedeoo
Posting Pro in Training
438 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: