IDIOT DEVELOPER

Reading XML files in PHP
Reading XML files in PHP
DOWNLOAD

Reading XML files means navigating through an XML file and returning the suitable data. Generally web services return data in JSON Object, but their are still web services that returns data in XML format. So you need to master parsing XML files to take full advantage of the web services.

 

What is XML

The eXtensible Markup Language is a way to structure your data for sharing across sites. Some of the technologies that are crucial to the web like RSS (Real Simple Syndication) and Podcasts are special flavors of XML. The beautiful thing about XML is that you can easily roll your own for anything you need.

 

XML is easy as it is very similar to HTML , so if you know HTML you can easily learn XML. In XML we can create our own tags, according to our need.  Suppose you want to keep a list of your students name, age, etc. For now we will only consider student name and age. Now we need to create tags.

<name> – for the student name.

<age> – for the student age.

and we are going to wrap student name and age in <student> tags, and all the student data will be wrapped around the <school> tags

 

student.xml

 


<?xml version="1.0" encoding="utf-8"?>
<school>
  <student>
    <name>Jackie</name>
    <age> 15 </age>
   </student>

  <student>
    <name>Dyna</name>
    <age> 14 </age>
  </student>

  <student>
    <name>Tony</name>
    <age> 16 </age>
  </student>
</school>

 

The first step is to loading the XML using either simplexml_load_file() or simplexml_load_string(). As you might expect, the former will load the XML file a file and the later will load the XML from a given string.

Both functions read the entire DOM tree into memory and returns a SimpleXMLElement object representation of it.

 


<?php
$student = simplexml_load_file('student.xml');
print_r($student);
?>

 

The above program runs and return SimpleXMLElement Object.

 


SimpleXMLElement Object ( 
  [student] => Array ( 
     [0] => SimpleXMLElement Object 
     ( 
       [name] => Jackie 
       [age] => 15 
     ) 
     [1] => SimpleXMLElement Object 
     ( 
       [name] => Dyna 
       [age] => 14 
     ) 
     [2] => SimpleXMLElement Object 
     ( 
       [name] => Tony 
       [age] => 16 
     ) 
   ) 
)

 

We can access the property of any object using¬†-> operator. For example –

 


<?php
$name = $xml->student[0]->name;
$age = $xml->student[0]->age;
echo $name . ' is ' . $age . ' years old. <br/>';
?>

we can also iterate through the Array of student.

 


<?php
foreach($xml->student as $data){
  $name = $data->name;
  $age = $data->age;
  echo $name . ' is ' . $age . ' years old. <br/>';
}
?>

 

readXML.php

 


<?php
$xml = simplexml_load_file('file.xml');

//First Student
$name = $xml->student[0]->name;
$age = $xml->student[0]->age;

echo $name . ' is ' . $age . ' years old. <br/>';

//Second Student
$name = $xml->student[1]->name;
$age = $xml->student[1]->age;

echo $name . ' is ' . $age . ' years old. <br/>';

//Third Student
$name = $xml->student[2]->name;
$age = $xml->student[2]->age;

echo $name . ' is ' . $age . ' years old. <br/>';

echo '<br/>';

//Read and Display data via loop
foreach($xml->student as $data){
  $name = $data->name;
  $age = $data->age;
  echo $name . ' is ' . $age . ' years old. <br/>';
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *