QAds
Nikon D90 DX 12.3MP Digital SLR Camera with 18-105mm f/3.5-5.6G ED AF-S VR DX Nikkor Zoom Lens
$0.69
Coby 2.8-Inch LCD Touchscreen Video MP3 Player
$52.49
Coby MP305-4G MP3 Player
$16.05
Apple iPod nano 8 GB Gumus
$91.00
:: HOME PHP How to Read XML file
 
How to Read XML file

If you use PHP 5, you can use simpleXML functions to read xml file easily. Using SimpleXMLElement class  makes it very simple.

 

<?PHP

$xmlstr= <<<XML

<?xml version='1.0' standalone='yes'?>

<products>

 <product>

  <productId>ABC-123</productId>

  <title>abc product</title>

   <price>12.00</price>

 </product>

 <product>

  <productId>ABC-123</productId>

  <title>abc product</title>

   <price>12.00</price>

 </product>

</products>

XML;

 

$xml = new SimpleXMLElement($xmlstr);

?>

 

After create xmlsimple object, we can get value with tag names.

 

$xml->product[0]->title;

 

That’s it. As you see, it is very simple. You just need to know the line number of data you want to pull. If you want to show second product title.

 

$xml->product[1]->title;

 

You can use foreach to pull all data.

 

Foreach($xml->product as $product)

{

Echo $product->title;

}

 

You can use simplexml_load_file() function to create xml object too.

 

$xml = simplexml_load_file("file.xml");

 

foreach($xml->product as $product)

echo $product->title.'<br>';

 

But simple xml doesn’t work with PHP 4. so you have to have PHP 5 to be able to use it.