note 66389 deleted from ref.simplexml by didou

From: Date: Tue, 16 Jan 2007 00:54:36 +0000
Subject: note 66389 deleted from ref.simplexml by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121773@lists.php.net to get a copy of this message
Note Submitter: djib ---- I wrote my own simplexml_to_array function which collects all information about attributes - cdata and childnodes of any level. I think it is much more simple compared to those I ran into so far, maybe it lacks a few things but it did the job for me... XML FILE: <?xml version="1.0" encoding="UTF-8" ?> <employees> <employee ID="1" FOO="BAR"> <empname>Anthony Clarke</empname> <emptitle>Chief Information Officer</emptitle> <empage>48</empage> </employee> <employee ID="2" BAZ="WOM"> <empname>Laura Pollard</empname> <emptitle>Chief Executive Officer</emptitle> <empage>54</empage> </employee> </employees> PHP CODE: function sxml_to_array($xml) { $arr = array(); $x = 0; foreach($xml as $a=>$b) { $arr[$a][$x] = array(); // Looking for ATTRIBUTES $att = $b->attributes(); foreach($att as $c=>$d) { $arr[$a][$x]['@'][$c] = (string) $d; } // Getting CDATA $arr[$a][$x]['cdata'] = trim((string) utf8_decode($b)); // Processing CHILD NODES $arr[$a][$x]['nodes'] = sxml_to_array($b); $x++; } return $arr; } $xml = simplexml_load_file('myfile.xml'); $arr = sxml_to_array($xml); print("<pre>"); print_r($arr); print("</pre>");

« previous php.notes (#121773) next »