note 87147 added to class.xmlreader
| From: | rein_baarsma33athotmaildotcom at osu1 dot php dot net | Date: | Fri, 21 Nov 2008 12:55:08 +0000 |
| Subject: | note 87147 added to class.xmlreader | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-146791@lists.php.net to get a copy of this message | ||
the post below has a bug for multiple the same tags
ex the following was not parsed correctly:
<rss>
..
<item>
..
</item>
<item>
..
</item
..
</rss>
I've fixed the bug in the code below:
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;
}
----
Server IP: 89.248.170.61
Probable Submitter: 82.93.102.254
----
Manual Page -- http://www.php.net/manual/en/class.xmlreader.php
Edit -- https://master.php.net/note/edit/87147
Del: integrated -- https://master.php.net/note/delete/87147/integrated
Del: useless -- https://master.php.net/note/delete/87147/useless
Del: bad code -- https://master.php.net/note/delete/87147/bad+code
Del: spam -- https://master.php.net/note/delete/87147/spam
Del: non-english -- https://master.php.net/note/delete/87147/non-english
Del: in docs -- https://master.php.net/note/delete/87147/in+docs
Del: other reasons-- https://master.php.net/note/delete/87147
Reject -- https://master.php.net/note/reject/87147
Search -- https://master.php.net/manage/user-notes.php