note 110165 added to class.xmlreader
| From: | MikeDeSmet | Date: | Tue, 25 Sep 2012 16:35:56 +0000 |
| Subject: | note 110165 added to class.xmlreader | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191015@lists.php.net to get a copy of this message | ||
For those of you getting xml files that do not contain duplicate elements (in the same element), the
following converter converts to arrays with key/value mapping (thus overwriting duplicate
elements!):
Note this is untested with attributes although I built in support.
function xml2assoc($xml, array &$target = array()) {
while ($xml->read()) {
switch ($xml->nodeType) {
case XMLReader::END_ELEMENT:
return $target;
case XMLReader::ELEMENT:
$name = $xml->name;
$target[$name] = $xml->hasAttributes ? array() : '';
if (!$xml->isEmptyElement) {
$target[$name] = array();
xml2assoc($xml, $target[$name]);
}
if ($xml->hasAttributes)
while($xml->moveToNextAttribute())
$target[$name]['@'.$xml->name] = $xml->value;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$target = $xml->value;
}
}
return $target;
}
----
Server IP: 69.147.83.199
Probable Submitter: 87.213.98.98
----
Manual Page -- http://www.php.net/manual/en/class.xmlreader.php
Edit -- https://master.php.net/note/edit/110165
Del: integrated -- https://master.php.net/note/delete/110165/integrated
Del: useless -- https://master.php.net/note/delete/110165/useless
Del: bad code -- https://master.php.net/note/delete/110165/bad+code
Del: spam -- https://master.php.net/note/delete/110165/spam
Del: non-english -- https://master.php.net/note/delete/110165/non-english
Del: in docs -- https://master.php.net/note/delete/110165/in+docs
Del: other reasons-- https://master.php.net/note/delete/110165
Reject -- https://master.php.net/note/reject/110165
Search -- https://master.php.net/manage/user-notes.php