search
top

Reading XML

I am listing here method for reading XML file or XML string with the help of PHP.

<?php
$xml =<<<EOT
<?xml version="1.0"?>
<root>
<section name="Section1">
    <category id="Category1" name="google">
    <article name="article1">value1</article>
    </category>  
    <category id="Category2" name="yahoo">
    <article name="articleSection2">Test value</article>
    </category>
</section>    
<section name="Section2">
  <category id="category1_of_section2" name="msn">
   <article name="article2">value1</article>
   <article name="article3">value2</article>
  </category>
    <category id="Category2_of_section2" name="dd">
    <article name="param3">value4</article>
   </category>
</section>
</root>
EOT;

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml); // Load XML File. You can use 'load' if you wish to load XML data from a xml file

$params = $dom->getElementsByTagName('section'); // Find Sections
$k=0;
foreach ($params as $param) //go to each section 1 by 1
{
        echo "Section Attribute :-> ".$params->item($k)->getAttribute('name')."<br>";   //get section attribute           
        $params2 = $params->item($k)->getElementsByTagName('category'); //categories within Section
        $i=0; // value is used to iterate categories 
        foreach ($params2 as $p) {
           echo "Category Attribute Name :-> ".$params2->item($i)->getAttribute('name')."<br>"; //get Category attributes
            $params3 = $params2->item($i)->getElementsByTagName('article'); //Article within Categories
                 $j=0;//values used to iterate Articles
                   foreach ($params3 as $p2)
                   {
                    echo "Article Attribute Name : ".$params3->item($j)->getAttribute('name')."<br/>"; //get article atributes
		    echo "Value:".$params3->item($j)->nodeValue."<br>"; //get Node value
                    $j++;  
                   }             
         $i++;
      }
$k++;   
         
}
?>

The output on executing above code will be:

Section Attribute :-> Section1
Category Attribute Name :-> google
Article Attribute Name : article1
Value:value1
Category Attribute Name :-> yahoo
Article Attribute Name : articleSection2
Value:Test value
Section Attribute :-> Section2
Category Attribute Name :-> msn
Article Attribute Name : article2
Value:value1
Article Attribute Name : article3
Value:value2
Category Attribute Name :-> dd
Article Attribute Name : param3
Value:value4

I am listing here another example:

<?php
$xml =<<<EOT
<?xml version="1.0"?>
<root>
<category>
  <id>1</id>
  <title>catname1</title>
  <desc>catname1 description</desc>  
  <udate>2010-01-1 03:50:22</udate>  
  <published>1</published>
</category>
<category>
  <id>2</id>
  <title>catname2</title>
  <desc>catname2 description</desc>    
  <udate>2010-02-2 03:50:22</udate>  
  <published>1</published>
</category>
</root>
EOT;

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$categories = $dom->getElementsByTagName("category"); // categories

foreach ($categories as $value) //go to each category 1 by 1
{	
    $values = $value->getElementsByTagName("*");		
    $reviewarr = array();
    foreach($values as $node) 
    {    	
    	$nodename = $node->nodeName;
     	$nodevalue = $node->nodeValue;
        echo $nodename.'&nbsp;::'.$nodevalue.'<br/>';
    }   
    	        
}

?>

The output on executing above code will be:

id ::1
title ::catname1
desc ::catname1 description
udate ::2010-01-1 03:50:22
published ::1
id ::2
title ::catname2
desc ::catname2 description
udate ::2010-02-2 03:50:22
published ::1

For further reference click here

Leave a Reply

top