Here is a PHP class that read xml file and make an array. First save this class file as xmltoarray.php and then create an object like new XmlToArray($xml_data). Finally just call createArray() function. It will be helpful when we don’t want to use database or any kind of web services. Bellow i have given full source code. I think it will be helpful.
<?
/**
* XMLToArray : Converting XML into Array
*
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @version : 1.0
* @date 11/03/2006
* Purpose : Creating Hierarchical Array from XML Data
*/
class XmlToArray
{
private $xml=”; //To use in PHP4, use “var”
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function XmlToArray($xml)
{
$this->xml = $xml;
}
/**
* _struct_to_array($values, &$i)
*
* This adds the contents of the return xml into the array for easier processing.
* Recursive, Static
*
* @access private
* @param array $values this is the xml data in an array
* @param int $i this is the current location in the array
* @return Array
*/
function _struct_to_array($values, &$i)
{
$child = array();
if (isset($values[$i][‘value’]))
{
array_push($child, $values[$i][‘value’]);
}
while ($i++ < count($values))
{
switch ($values[$i][‘type’])
{
case ‘cdata’:
array_push($child, $values[$i][‘value’]);
break;
case ‘complete’:
$name = $values[$i][‘tag’];
if(!empty($name))
{
$child[$name]= ($values[$i][‘value’])?($values[$i][‘value’]):”;
if(isset($values[$i][‘attributes’]))
{
$child[$name] = $values[$i][‘attributes’];
}
}
break;
case ‘open’:
$name = $values[$i][‘tag’];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;
case ‘close’:
return $child;
break;
}
}
return $child;
}//EO _struct_to_array
/**
* createArray($data)
*
* This adds the contents of the return xml into the array for easier processing.
*
* @access public
* @param string $data this is the string of the xml data
* @return Array
*/
function createArray()
{
$xml = $this->xml;
$values = array();
$index = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xml, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i][‘tag’];
$array[$name] = isset($values[$i][‘attributes’]) ? $values[$i][‘attributes’] : ”;
$array[$name] = $this->_struct_to_array($values, $i);
return $array;
}//createArray
}//EO Class XmlToArray
?>
<?php
/**
* phpResourceRssFeed : phpResource Feeds for Your Site
*
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
* @version : 1.0
* @date 11/03/2006
*/
?>
<title>phpResource Feeds for Your Site</title>
<?php
require_once(“XmlToArray.class.php”);
$xml_data = file_get_contents(“http://rss.groups.yahoo.com/group/phpresource/rss”);
//Creating Instance of the Class
$xmlObj = new XmlToArray($xml_data);
//Creating Array
$arrayData = $xmlObj->createArray();
$rssData = array();
$rssData = $arrayData[‘rss’][‘channel’][0][‘item’];
$colors = array(‘#CCCCCC’,’#f5f5f5′);
$numColors = count($colors);
if(!empty($rssData))
{
?>
<table align=”left”>
<tr>
<td> </td>
</tr>
<tr bgcolor=”#6699CC”>
<td colspan=”2″><font size=”5″><b>Latest Posts of <a href=”http://groups.yahoo.com/group/phpresource/”>phpResource Group</a></b></font></td>
</tr>
<tr>
<td colspan=”2″> </td>
</tr>
<?php
$i = 0;
foreach($rssData as $i=>$v)
{
$title = $v[‘title’];
$link = $v[‘link’];
$author = $v[‘author’];
$pubDate = $v[‘pubDate’];
$desc = $v[‘description’];
?>
<tr bgcolor=”<?=$colors[$i%$numColors];?>”>
<td><a href=”<?=$link?>”><font size=”4″><?=$title?></font></a><br /> On <?=$pubDate?> By <b><?=$author?></b></td>
</tr>
<tr>
<td>
<?php
$desc = wordwrap( $desc, 72, “<br>”);
echo “$desc …. “;
//echo $desc.’ <b>…</b> ‘;
?>
<br />
<a href=”<?=$link?>”>Read More…</a>
</td>
</tr>
<tr>
<td> </td>
</tr>
<?php
$i++;
}
}
function dBug($arr)
{
echo “<PRE>”;
print_r($arr);
echo “</PRE>”;
}
?>
<tr bgcolor=”#6699CC”>
<td><a href=”http://groups.yahoo.com/group/phpresource/join/”><font size=”5″>Click Here to Join phpResource</font></a></td>
</tr>
</table>
Sodgiliingedy
Engaging post. Hope to visit soon.
JamesDX
Anyone know how to do things like this?
MIKI
Could you help me using the instruccion:
$rssData = $arrayData[‘ArrayOfArrayOfString’][‘ArrayOfString’];
in order to read this xml and to save the content of the tags.
3459629
60
20100614
Thank’s
rantotthus
thanks, just what i needed!
i found a bug:
$child[$name]= ($values[$i][‘value’])?($values[$i][‘value’]):”;
this line should include an isset, like this
$child[$name]= isset($values[$i][‘value’])?($values[$i][‘value’]):”;
because 0 values were not present in the result.