PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

13 Oct 2014

Parse / read data from XML file using PHP

                                 You can get value from XML using PHP in following two ways.

1. Get data from XML using simple_load_file() in PHP
2. Get data from XML using file_get_contents() and SimpleXMLElement() in PHP

Lets see below examples.
 If the XML file looks like below, then you can retrieve node values from XML file using following two ways.

<?xml version="1.0" encoding="utf-8"?>
 <userlist>
    <user>
       <name>Clark</name>
       <state>California</state>
       <country>USA</country>
   </user>
   <user>
      <name>Smith</name>
      <state>London</state>
      <country>United Kingdom</country>
   </user>
   <user>
      <name>Nathan</name>
      <state>Franche Comte</state>
      <country>France</country>
   </user>
   <user>
      <name>Nastya</name>
      <state>Moscow</state>
      <country>Russia</country>
   </user>
</userlist>

Get data from XML  using simple_load_file() in PHP


                                   Simple_load_file() in PHP is used to convert XML document into an object. The below example explain retrieve data from XML file.


<?php
$xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML file using children() with simplexml_load_file() in PHP

          
                       The children() find the children of the specific node. Consider below example to parsing XML values using PHP.


<?php
 $xml=simplexml_load_file('sample.xml');
 //print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml->children() as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>

Get data from XML using file_get_contents and SimpleXMLElement in PHP


                                  File_get_contents() is used to read the contents of a file into string. SimpleXMLElement is used to represents elements of XML document. The below example explain retrieve data from XML file.

<?php
$url=file_get_contents('sample.xml');
 $xml=new SimpleXMLElement($url);
// print_R($xml);
 echo'<table><tr><th>Name</th><th>State</th><th>Country</th></tr>';
 foreach($xml as $user)
 {
  echo'<tr><td>'.$user->name.'</td><td>'.$user->state.'</td><td>'.$user->country.'</td></tr>';
 }
 echo'</table>';
?>


 When you give print_R() in above PHP file, you can see the output on screen like below.


  SimpleXMLElement Object (
  [user] => Array (
     [0] => SimpleXMLElement Object (
   [name] => Clark
[state] => California
[country] => USA )
[1] => SimpleXMLElement Object (
   [name] => Smith
[state] => London
[country] => United Kingdom )
[2] => SimpleXMLElement Object (
   [name] => Nathan
[state] => Franche Comte
[country] => France )
[3] => SimpleXMLElement Object (
   [name] => Nastya
[state] => Moscow
[country] => Russia )
 )
)



                You can get output as below

NameStateCountry
ClarkCaliforniaUSA
SmithLondonUnited Kingdom
NathanFranche ComteFrance
NastyaMoscowRussia
 
Now you can retrieve data from XML file. You can parsed data from XML using PHP.