0

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

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by veedeoo
0

Thx for that.

Any coding example maybe?

I did search online but couldnt sort it out..

0

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

}

Edited by veedeoo: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.