note 73811 added to function.simplexml-element-attributes

From: Date: Sun, 11 Mar 2007 16:28:47 +0000
Subject: note 73811 added to function.simplexml-element-attributes
Groups: php.notes 
Request: Send a blank email to php-notes+get-124346@lists.php.net to get a copy of this message
I expanded a bit (not very impressive, I admit) on the user submitted class for extending simple xml. Below are some nice quick and dirty additional class functions I have needed to manage the back end for plugging in flash parameters to dynamic actionscripted .swf files needing this xml data generated dynamically and with working variable xml content data that the client maintains (yikes!). Probably need some bad character checking in here somewhere? <?php $xml_data = '<?xml version="1.0" encoding="ISO8859-1" ?> <root> <image name="Banner 1" file="pic1.jpg" /> <image name="Banner 2" file="pic2.jpg" /> <image name="Banner 3" file="pic3.jpg" /> <image name="Banner 4" file="pic4.jpg" /> </root>'; class SimpleXMLElementExtended extends SimpleXMLElement{ public function getAttribute($name){ foreach($this->attributes() as $key=>$val){ if($key == $name){ return (string)$val; }// end if }// end foreach }// end function getAttribute public function getAttributeNames(){ $cnt = 0; $arrTemp = array(); foreach($this->attributes() as $a => $b) { $arrTemp[$cnt] = (string)$a; $cnt++; }// end foreach return (array)$arrTemp; }// end function getAttributeNames public function getChildrenCount(){ $cnt = 0; foreach($this->children() as $node){ $cnt++; }// end foreach return (int)$cnt; }// end function getChildrenCount public function getAttributeCount(){ $cnt = 0; foreach($this->attributes() as $key=>$val){ $cnt++; }// end foreach return (int)$cnt; }// end function getAttributeCount public function getAttributesArray($names){ $len = count($names); $arrTemp = array(); for($i = 0; $i < $len; $i++){ $arrTemp[$names[$i]] = $this->getAttribute((string)$names[$i]); }// end for return (array)$arrTemp; }// end function getAttributesArray } $xml2 = new SimpleXMLElementExtended($xml_data); print('<pre>'); print('NUMBER OF CHILDREN: '.$xml2->getChildrenCount()); print('</pre>'); print('<pre>'); print('ATTRIBUTE COUNT PER CHILD: '.$xml2->image[0]->getAttributeCount()); print('</pre>'); print('<pre>'); print('CHILD 0 ATTRIBUTE "name": '.$xml2->image[0]->getAttribute('name')); print('</pre>'); print('<pre>'); print('CHILD 0 ATTRIBUTE "file": '.$xml2->image[0]->getAttribute('file')); print('</pre>'); $arrTemp = array('name', 'file'); $arrResult = array(); $arrResult = $xml2->image[0]->getAttributesArray($arrTemp); print('<pre>'); print('CHILD 0 ATTRIBUTE ARRAY: "name,file":<br>'); print(var_dump($arrResult)); print('</pre>'); $arrResult = array(); $arrResult = $xml2->image[0]->getAttributeNames(); print('<pre>'); print('CHILD 0 ATTRIBUTE NAMES ARRAY:<br>'); print(var_dump($arrResult)); print('</pre>'); print('<hr>'); $arrCompiled = array(); $arrCompiled = $xml2->image[0]->getAttributesArray($arrResult); print('<pre>'); print('CHILD 0 DYNAMIC COMPILED ATTRIBUTE ARRAY: "key,value"<br>'); print(var_dump($arrCompiled)); print('</pre>'); ?> Outputs something along these lines below: NUMBER OF CHILDREN: 4 ATTRIBUTE COUNT PER CHILD: 2 CHILD 0 ATTRIBUTE "name": Banner 1 CHILD 0 ATTRIBUTE "file": pic1.jpg CHILD 0 ATTRIBUTE ARRAY: "name,file": array(2) { ["name"]=> string(8) "Banner 1" ["file"]=> string(8) "pic1.jpg" } CHILD 0 ATTRIBUTE NAMES ARRAY: array(2) { [0]=> string(4) "name" [1]=> string(4) "file" } CHILD 0 DYNAMIC COMPILED ATTRIBUTE ARRAY: "key,value" array(2) { ["name"]=> string(8) "Banner 1" ["file"]=> string(8) "pic1.jpg" } I'm sure there is more to add/revise/simplify, but it's a fast start to working with fairly simple xml schema/structures. Would be interested is anyone knows of how we could revise this class to say perhaps evaluate the nodes AND attributes for the same xml element... like image id attribute with say 4 image file name elements? Peace. ---- Server IP: 69.147.83.197 Probable Submitter: 76.183.228.184 ---- Manual Page -- http://www.php.net/manual/en/function.simplexml-element-attributes.php Edit -- https://master.php.net/note/edit/73811 Del: integrated -- https://master.php.net/note/delete/73811/integrated Del: useless -- https://master.php.net/note/delete/73811/useless Del: bad code -- https://master.php.net/note/delete/73811/bad+code Del: spam -- https://master.php.net/note/delete/73811/spam Del: non-english -- https://master.php.net/note/delete/73811/non-english Del: in docs -- https://master.php.net/note/delete/73811/in+docs Del: other reasons-- https://master.php.net/note/delete/73811 Reject -- https://master.php.net/note/reject/73811 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124346) next »