www ေအာက္မွာ(in wamp server) folder တစ္ခုေဆာက္ၿပီး ဒီ xml code ေတြကို notepad မွာ pase လုပ္ၿပီး address.xml ဆိုတဲ့အမည္နဲ႔ save လိုက္ပါ။
<?xml version=”1.0″ encoding=”utf-8″?>
<users>
<user>
<firstname>Sheila</firstname>
<surname>Green</surname>
<address>2 Good St</address>
<city>Campbelltown</city>
<country>Australia</country>
<contact>
<phone>1234 1234</phone>
<url>http://example.com</url>
<email>pamela@example.com</email>
</contact>
</user>
<user>
<firstname>Bruce</firstname>
<surname>Smith</surname>
<address>1 Yakka St</address>
<city>Meekatharra</city>
<country>Australia</country>
<contact>
<phone>4444 4444</phone>
<url>http://yakka.example.com</url>
<email>bruce@yakka.example.com</email>
</contact>
</user>
<user>
<firstname>Davo</firstname>
<surname>White</surname>
<address>The Only Way</address>
<city>Whitefall</city>
<country>Australia</country>
<contact>
<phone>8888 8888</phone>
<url>http://white.example.com</url>
<email>davo@white.example.com</email>
</contact>
</user>
<user>
<firstname>Shazza</firstname>
<surname>Green</surname>
<address>222 Great Western Hwy</address>
<city>Bathurst</city>
<country>Australia</country>
<contact>
<phone>6666 6666</phone>
<url>http://shazza.green.example.com</url>
<email>shazza@green.example.com</email>
</contact>
</user>
</users>

ၿပီးရင္ ဒီphp code ေတြကို notepad မွာ pase လုပ္ၿပီး index.php ဆိုၿပီး save လုပ္လိုက္ပါ။

<?php

if( ! $xml = simplexml_load_file(‘address.xml’) )
{
echo ‘unable to load XML file’;
}
else
{
foreach( $xml as $user )
{
echo ‘Firstname: ‘.$user->firstname.'<br />’;
echo ‘Surname: ‘.$user->surname.'<br />’;
echo ‘Address: ‘.$user->address.'<br />’;
echo ‘City: ‘.$user->city.'<br />’;
echo ‘Country: ‘.$user->country.'<br />’;
echo ‘Email: ‘.$user->contact->phone.'<br />’;
echo ‘Email: ‘.$user->contact->url.'<br />’;
echo ‘Email: ‘.$user->contact->email.'<br /><br />’;
}
}
?>

ဒါကို browser မွာ run ရင္ေတာ႔ xml data ကို php မွာ ဘယ္လိုေခၚယူအသံုးခ်တယ္ဆိုတာသေဘာေပါက္သြားမွာပါ။ ဒီ example မွာအဓိကကေတာ႔ simplexml_load_file() ဆိုတဲ့ php ရဲ႔ function ကိုေခၚယူအသံုးခ်ျခင္းပါဘဲ။ file ကမဟုတ္ဘဲ string ကေခၚရင္ေတာ႔ simplexml_load_string() ကိုသံုးပါတယ္။
like this

<?php
$xml_string = ‘<?xml version=”1.0″ encoding=”iso-8859-1″?>
<users>
<user>
<firstname>Sheila</firstname>
<surname>Green</surname>
<address>2 Good St</address>
<city>Campbelltown</city>
<country>Australia</country>
<contact>
<phone>1234 1234</phone>
<url>http://example.com</url>
<email>pamela@example.com</email>
</contact>
</user>
</users>’;

if( ! $xml = simplexml_load_string( $xml_string ) )
{
echo ‘Unable to load XML string’;
}
else
{
echo ‘XML String loaded successfully’;
}

?>
Ref:phppro.com

About wailynnoo

wailynn oo has written 57 post in this Website..

I am a blogger.