note 83781 added to ref.simplexml
| From: | SmartD at osu1 dot php dot net | Date: | Wed, 11 Jun 2008 15:40:24 +0000 |
| Subject: | note 83781 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140349@lists.php.net to get a copy of this message | ||
A small 'n nice function to extract an XML and return it as an array. If there is a bug, please
let me know here. I testet it for my purposes and it works.
<?php
public function extractXML($xml) {
if (!($xml->children())) {
return (string) $xml;
}
foreach ($xml->children() as $child) {
$name=$child->getName();
if (count($xml->$name)==1) {
$element[$name] = $this->extractXML($child);
} else {
$element[][$name] = $this->extractXML($child);
}
}
return $element;
}
// you can call it this way
$xml = false;
$xml = @simplexml_load_string($xmlstring);
// 1)
if ($xml) {
$array = extractXML($xml);
} else {
$array = false;
}
// 2)
if ($xml) {
$array[$xml->getName()] = extractXML($xml);
} else {
$array = false;
}
?>
----
Server IP: 217.160.72.57
Probable Submitter: 80.146.215.43
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/83781
Del: integrated -- https://master.php.net/note/delete/83781/integrated
Del: useless -- https://master.php.net/note/delete/83781/useless
Del: bad code -- https://master.php.net/note/delete/83781/bad+code
Del: spam -- https://master.php.net/note/delete/83781/spam
Del: non-english -- https://master.php.net/note/delete/83781/non-english
Del: in docs -- https://master.php.net/note/delete/83781/in+docs
Del: other reasons-- https://master.php.net/note/delete/83781
Reject -- https://master.php.net/note/reject/83781
Search -- https://master.php.net/manage/user-notes.php