note 103173 added to book.simplexml
| From: | marcus dot garcia at uol dot com dot br | Date: | Tue, 29 Mar 2011 21:34:10 +0000 |
| Subject: | note 103173 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178329@lists.php.net to get a copy of this message | ||
It's IMPRESSIVE how people like to post half-assed functions in here, spreading broken code
like a virus, just like svdmeer at xs4all dot nl and many others below.
Well, here's decente version of xml2array, working AND keeping all the attributes. also taking
account of duplicated names etc:
function xml2array($xml) {
if(get_class($xml) != 'SimpleXMLElement') {
$xml = simplexml_load_string($xml);
}
if(!$xml) {
return false;
}
$main = $xml->getName();
$arr = array();
$nodes = $xml->children();
foreach($nodes as $node) {
$nodeName = $node->getName();
$nodeAttributes = $node->attributes();
$attributesArray = array();
foreach($nodeAttributes as $attributeName => $attributeValue) {
$attributesArray[$attributeName] = (string) $attributeValue;
}
$nodeValue = sizeOf($node->children()) == 0 ? trim($node) : xml2array($node);
if(!isSet($arr[$nodeName]['value'])) {
$arr[$nodeName]['value'] = $nodeValue;
$arr[$nodeName]['attributes'] = $attributesArray;
} else {
if(!is_array($arr[$nodeName]['value'])) {
$arr[$nodeName]['value'][] = array_shift($arr[$nodeName]);
$arr[$nodeName]['attributes'][] =
array_shift($arr[$nodeName]['attributes']);
}
$arr[$nodeName]['value'][] = $nodeValue;
$arr[$nodeName]['attributes'][] = $attributesArray;
}
}
return($arr);
}
So the code above does exactly what it's supposed to do: Take a XML string and return an array
out of that;
Returns false if the XML is not valid or parseable by SimpleXML for any reason, else, returns an
array containing the nodes of the XML, keeping in mind:
It's not an array of Name => Value
It IS an array of
(String) Node name = {
Value = String (node value) OR Array(inner nodes)
Attributes = {
(String) Atribute name 1 = (String) attribute value
(String) Atribute name 2 = (String) attribute value
}
}
An example of:
<?xml version="1.0" encoding="UTF-8"
standalone="yes"?><main>
<inner abc="def" ghi="jkl">
<innerInner>text</innerInner>
<innerInner_DUP kkk="lll">text</innerInner_DUP>
<innerInner_DUP xtz="ztx" zzz="xxx">text</innerInner_DUP>
</inner>
<inner2>
<innerInner2 abc="def">text2</innerInner2>
</inner2>
</main>
Will return this:
Array
(
[inner] => Array (
[value] => Array (
[innerInner] => Array (
[value] => text
[attributes] => Array ( )
)
[innerInner_DUP] => Array (
[attributes] => Array (
[0] => lll
[1] => Array
(
[xtz] => ztx
[zzz] => xxx
)
)
[value] => Array (
[0] => text
[1] => text
)
)
)
[attributes] => Array (
[abc] => def
[ghi] => jkl
)
)
[inner2] => Array (
[value] => Array (
[innerInner2] => Array (
[value] => text2
[attributes] => Array (
[abc] => def
)
)
)
[attributes] => Array ( )
)
)
Which preserves every single node name, value and attributes of the XML, which i find years ahead of
half-assed implementations with variable naming such as $t .
Keeps everything easy to understand, structurally correct etc.
----
Server IP: 69.147.83.197
Probable Submitter: 201.83.61.187
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/103173
Del: integrated -- https://master.php.net/note/delete/103173/integrated
Del: useless -- https://master.php.net/note/delete/103173/useless
Del: bad code -- https://master.php.net/note/delete/103173/bad+code
Del: spam -- https://master.php.net/note/delete/103173/spam
Del: non-english -- https://master.php.net/note/delete/103173/non-english
Del: in docs -- https://master.php.net/note/delete/103173/in+docs
Del: other reasons-- https://master.php.net/note/delete/103173
Reject -- https://master.php.net/note/reject/103173
Search -- https://master.php.net/manage/user-notes.php