<?php
/*
XML Parser Class
by Eric Rosebrock
modified by Richard Bishop 30/05/2007
http://www.phpfreaks.com
Class originated from: kris@h3x.com AT: http://www.devdump.com/phpxml.php
Usage:
<?php
include 'clsParseXML.php';
$xmlparse = &new ParseXML;
$xml = $xmlparse->GetXMLTree('/path/to/xmlfile.xml');
echo "<pre>";
print_r($xml);
echo "</pre>";
?>
The path to the XML file may be a local file or a URL.
Returns the elements of the XML file into an array with
it's subelements as keys and subarrays.
*/
class clsParseXML{
function GetChildren($vals, &$i) {
$children = array(); // Contains node data
if (isset($vals[$i]['value'])){
$children['VALUE'] = $vals[$i]['value'];
}
while (++$i < count($vals)){
switch ($vals[$i]['type']){
case 'cdata':
if (isset($children['VALUE'])){
$children['VALUE'] .= $vals[$i]['value'];
} else {
$children['VALUE'] = $vals[$i]['value'];
}
break;
case 'complete':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
if (isset($vals[$i]['value'])){
$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
} else {
$children[$vals[$i]['tag']][$index]['VALUE'] = '';
}
} else {
if (isset($vals[$i]['value'])){
$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
} else {
$children[$vals[$i]['tag']][]['VALUE'] = '';
}
}
break;
case 'open':
if (isset($vals[$i]['attributes'])) {
$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($children[$vals[$i]['tag']])-1;
$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
} else {
$children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
break;
case 'close':
return $children;
}
}
}
function GetXMLDataTree($data){
$parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$tree = array();
$i = 0;
if (isset($vals[$i]['attributes'])) {
$tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($tree[$vals[$i]['tag']])-1;
$tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
} else {
$tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
return $tree;
}
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);
}
$parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$tree = array();
$i = 0;
if (isset($vals[$i]['attributes'])) {
$tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
$index = count($tree[$vals[$i]['tag']])-1;
$tree[$vals[$i]['tag']][$index] = array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
} else {
$tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
}
return $tree;
}
}
?>
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
preferences:
263.77 ms | 406 KiB | 461 Q