note 66389 added to ref.simplexml

From: Date: Sun, 21 May 2006 02:02:49 +0000
Subject: note 66389 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-111747@lists.php.net to get a copy of this message
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>"); ---- Server IP: 66.163.161.117 Probable Submitter: 80.200.207.45 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- http://master.php.net/note/edit/66389 Del: integrated -- http://master.php.net/note/delete/66389/integrated Del: useless -- http://master.php.net/note/delete/66389/useless Del: bad code -- http://master.php.net/note/delete/66389/bad+code Del: spam -- http://master.php.net/note/delete/66389/spam Del: non-english -- http://master.php.net/note/delete/66389/non-english Del: in docs -- http://master.php.net/note/delete/66389/in+docs Del: other reasons-- http://master.php.net/note/delete/66389 Reject -- http://master.php.net/note/reject/66389 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111747) next »