|
|
|
|
|
|
| |
This class can be used to parse XML documents.
The parser results are stored in an associative array with the whole document structure.
Each array entry may contain nested arrays that describe the contents of the document tag structure.
The resultant array contains the exact hierarchy as of the given XML data.
class.xmltoarray.php
|
<?
/**
* XMLToArray Generator Class
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
* URL: LINK2http://www.rupom.infoLINK2
* @version : 1.0
* @date 06/05/2006
* Purpose : Creating Hierarchical Array from XML Data
* Released : Under GPL
*/
class XmlToArray
{
var $xml='';
/**
* Default Constructor
* @param $xml = xml data
* @return none
*/
function XmlToArray($xml)
{
$this->xml = $xml;
}
/**
* _struct_to_array($values, &$i)
*
* This is 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;
}//_struct_to_array
/**
* createArray($data)
*
* This is 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
}//XmlToArray
?> | |
Usage Example
| <?php
/**
* XMLToArray Generator Class
* @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 06/05/2006
* Purpose : Creating Hierarchical Array from XML Data
* Released : Under GPL
*/
require_once "class.xmltoarray.php";
//XML Data
$xml_data = "
<result>
<studentname>
MA Razzaque
</studentname>
<institute>
RUET
</institute>
<dept>
CSE
</dept>
<roll>
99315
</roll>
<class>
First
</class>
</result>";
//Creating Instance of the Class
$xmlObj = new XmlToArray($xml_data);
//Creating Array
$arrayData = $xmlObj->createArray();
//Displaying the Array
echo "<pre>";
print_r($arrayData);
echo "</pre>";
?> | | |
| DBXML- A Class to backup databases in XML Format using web interface Categories : PHP, PHP Classes, Databases, MySQL, XML | | | PHPDRAW, the php wannabe Photoshop ;-) Categories : PHP, PHP Classes, GD image library, Arrays | | | XML Menu Categories : PHP, PHP Classes, Navigation, XML, XSL | |
| | Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it. Categories : PHP Classes, Arrays, PHP | | | logger class (PHP5 +) Categories : PHP, PHP Classes, Log Files, XML | | | TAB_STRUCT Class: Is supporting Class for the DBXML Class Categories : PHP, PHP Classes, MySQL, XML, Databases | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | XML easy parser Categories : PHP, XML, PHP Classes | | | XPath for PHP without the DOM XML extension Categories : DOM XML, XML, XSLT, PHP Classes, PHP | | | A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms, Variables, Arrays, PHP, PHP Classes | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | Directory Listing To XML : Outputs XML File of a Given Directory Listing Categories : PHP, PHP Classes, XML, Directories | | | Freshmeat.net XML-RPC - This class is meant to query Freshmeat for information about registered projects. Categories : PHP, PHP Classes, XML, Web Services | | | [PHP5] PHP Debugger and Helper Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML | |
|
|