note 86953 modified in class.xmlreader by danbrown
| From: | danbrown@php.net | Date: | Fri, 21 Nov 2008 14:44:34 +0000 |
| Subject: | note 86953 modified in class.xmlreader by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146796@lists.php.net to get a copy of this message | ||
There is a bug in the previous author's function. If a tag without children contains both
attributes and a value, the function skips the attributes.
Here's an improved version that keeps both:
<?php
$xml = new XMLReader();
function parseXml($xml) {
$assoc = null;
$dc = -1;
while($xml->read()){
switch ($xml->nodeType) {
case XMLReader::END_ELEMENT: return $assoc;
case XMLReader::ELEMENT:
if(!isset($assoc[$xml->name])) {
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name] = '';
} else {
$assoc[$xml->name] = parseXml($xml);
}
}
} elseif (is_array($assoc[$xml->name])) {
if (!isset($assoc[$xml->name][0]))
{
$temp = $assoc[$xml->name];
foreach ($temp as $key=>$val)
unset($assoc[$xml->name][$key]);
$assoc[$xml->name][] = $temp;
}
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name][] = '';
} else {
$assoc[$xml->name][] = parseXml($xml);
}
}
} else {
$mOldVar = $assoc[$xml->name];
$assoc[$xml->name] = array($mOldVar);
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name][] = '';
} else {
$assoc[$xml->name][] = parseXml($xml);
}
}
}
if($xml->hasAttributes) {
$el =& $assoc[$xml->name][count($assoc[$xml->name]) - 1];
while($xml->moveToNextAttribute()) {
$el[$xml->name] = $xml->value;
}
}
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
//$assoc .= $xml->value;
$assoc[++$dc] = $xml->value;
}
}
return $assoc;
}
?>
[EDIT BY danbrown AT php DOT net: The previous author was godseth AT o2 DOT pl. His note was
removed. This version of the note also contains bugfixes contributed by rein_baarsma33 AT hotmail
DOT com.]
--was--
There is a bug in the previous author's function. If a tag without children contains both
attributes and a value, the function skips the attributes.
-- Editor's note: The previsou author was godseth at o2 dot pl. His note was removed. --
Here's an improved version that keeps both:
<?php
$xml = new XMLReader();
function parseXml($xml) {
$assoc = null;
$dc = -1;
while($xml->read()){
switch ($xml->nodeType) {
case XMLReader::END_ELEMENT: return $assoc;
case XMLReader::ELEMENT:
if(!isset($assoc[$xml->name])) {
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name] = '';
} else {
$assoc[$xml->name] = parseXml($xml);
}
}
} elseif (is_array($assoc[$xml->name])) {
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name][] = '';
} else {
$assoc[$xml->name][] = parseXml($xml);
}
}
} else {
$mOldVar = $assoc[$xml->name];
$assoc[$xml->name] = array($mOldVar);
if($xml->hasAttributes) {
$assoc[$xml->name][] = $xml->isEmptyElement ? '' :
parseXml($xml);
} else {
if($xml->isEmptyElement) {
$assoc[$xml->name][] = '';
} else {
$assoc[$xml->name][] = parseXml($xml);
}
}
}
if($xml->hasAttributes){
$el =& $assoc[$xml->name][count($assoc[$xml->name]) - 1];
while($xml->moveToNextAttribute()) {
$el[$xml->name] = $xml->value;
}
}
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
//$assoc .= $xml->value;
$assoc[++$dc] = $xml->value;
}
}
return $assoc;
}
?>
http://php.net/manual/en/class.xmlreader.php