<?php
// Developed by : P. Shunmuga prasath, Prabhakar and Ravi
// Created on : 19-Mar-2007
// To parse the xml and returns the array
class ParseXML{
// To get the child nodes
function GetChildren($vals, &$incre) {
$children = array(); // Contains node data
if (isset($vals[$incre]['value'])){
$children['value'] = $vals[$incre]['value'];
}
while (++$incre < count($vals)){
switch ($vals[$incre]['type']){
case 'cdata':
if (isset($children['value'])){
$children['value'] .= $vals[$incre]['value'];
} else {
$children['value'] = $vals[$incre]['value'];
}
break;
case 'complete':
if (isset($vals[$incre]['attributes'])) {
$children[$vals[$incre]['tag']][]['attributes'] = $vals[$incre]['attributes'];
$index = count($children[$vals[$incre]['tag']])-1;
if (isset($vals[$incre]['value'])){
$children[$vals[$incre]['tag']][$index]['value'] = $vals[$incre]['value'];
} else {
$children[$vals[$incre]['tag']][$index]['value'] = '';
}
} else {
if (isset($vals[$incre]['value'])){
$children[$vals[$incre]['tag']][]['value'] = $vals[$incre]['value'];
} else {
$children[$vals[$incre]['tag']][]['value'] = '';
}
}
break;
case 'open':
if (isset($vals[$incre]['attributes'])) {
$children[$vals[$incre]['tag']][]['attributes'] = $vals[$incre]['attributes'];
$index = count($children[$vals[$incre]['tag']])-1;
$children[$vals[$incre]['tag']][$index] = array_merge($children[$vals[$incre]['tag']][$index],$this->GetChildren($vals, $incre));
} else {
$children[$vals[$incre]['tag']][] = $this->GetChildren($vals, $incre);
}
break;
case 'close':
return $children;
}
}
}
function GetXMLTree($xmlloc){
if (file_exists($xmlloc)){
$data = implode('', file($xmlloc));
} else {
$fp = fopen($xmlloc,'r');
while(!feof($fp)){
$data = $data . fread($fp, 1024);
}
fclose($fp);
}
$data = $this->_translateLiteral2NumericEntities($data);
$parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$tree = array();
$incre = 0;
if (isset($vals[$incre]['attributes'])) {
$tree[$vals[$incre]['tag']][]['attributes'] = $vals[$incre]['attributes'];
$index = count($tree[$vals[$incre]['tag']])-1;
$tree[$vals[$incre]['tag']][$index] = array_merge($tree[$vals[$incre]['tag']][$index], $this->GetChildren($vals, $incre));
} else {
$tree[$vals[$incre]['tag']][] = $this->GetChildren($vals, $incre);
}
return $tree;
}
function _translateLiteral2NumericEntities($xmlSource, $reverse =
FALSE) {
static $literal2NumericEntity;
if (empty($literal2NumericEntity)) {
$transTbl = get_html_translation_table(HTML_ENTITIES);
foreach ($transTbl as $char => $entity) {
if (strpos('&"<>', $char) !== FALSE) continue;
$literal2NumericEntity[$entity] = '&#'.ord($char).';';
}
}
if ($reverse) {
return strtr($xmlSource, array_flip($literal2NumericEntity));
} else {
return strtr($xmlSource, $literal2NumericEntity);
}
}
}