QAds
12 Ay Taksit
Sony Cybershot DSC-W220 12.1MP Digital Camera with 4x Optical Zoom
$72.41
Motorola A1200 MING Smartphone
$77.58
Coby MP305-2G MP3 Player
$8.95
Samsung SL30 10MP Digital Camera with 3x Optical Zoom and 2.5 inch LCD
$48.10
:: 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.

 


PHP How to Read XML file PHP How to Read XML file PHP How to Read XML file
PHP How to Read XML file
NEW QUESTION
 
Your E-Mail Address
»
Your e-mail will never be shown!  
   
 
Question
»
Please type your question and please use [CODE] tag for your codes!  
  Add CODE Quote BOLD ITALIC Underline Align Left Align Center Align Right Add Line  
   
 
Human Check
»
Enter human check code below!  
   
 
       
 
* Your Question will be answered as soon as possible, probably within 24 hours!
PHP How to Read XML file PHP How to Read XML file PHP How to Read XML file
PHP How to Read XML file
 
Website Designer, Search Engine Optimization Expert, Ajax Developer,Developed And Designed By Mumin Celik With Using PHP, MySQL, CSS, JavaScript, Ajax And Search Engine Optimizer Sinema ve Film Rehberi Sudoku Oyunu