note 73813 added to function.simplexml-element-children

From: Date: Sun, 11 Mar 2007 19:16:46 +0000
Subject: note 73813 added to function.simplexml-element-children
Groups: php.notes 
Request: Send a blank email to php-notes+get-124348@lists.php.net to get a copy of this message
Another interesting way to dynamically create associative arrays by key name is to use a customized class as follows: <?php // turn off magic quotes for this script's entire routine set_magic_quotes_runtime(0); // general configurations, variables, assignments $debug_mode = 1; // xml file to be loaded $xml_file = 'xml/test.xml'; // custom class for getting various xml elements/attributes/results 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 public function getChildrenValues(){ $arrTemp = array(); foreach($this->children() as $a => $b) { $arrTemp[(string)$a] = (string)$b; }// end foreach return (array)$arrTemp; }// end function getChildrenValues }// end class SimpleXMLElementExtended extends SimpleXMLElement // get the xml data loaded and parsed and ready for use with rest of scripts // get the fully cleaned and prepped xml data string $xml_data = '<?xml version="1.0" encoding="ISO8859-1" ?> <root> <image> <name>Banner 1</name> <file>file1.jpg</file> </image> <image> <name>Banner 2</name> <file>file2.jpg</file> </image> <image> <name>Banner 3</name> <file>file3.jpg</file> </image> <image> <name>Banner 4</name> <file>file4.jpg</file> </image> </root>'; // output the file if debug is on if($debug_mode == 1){ print('<pre>'); print(htmlentities($xml_data)); print('</pre>'); } // 2 LEVEL down without Attributes for Children Nodes $xml = new SimpleXMLElementExtended($xml_data); print('<pre>'); print('CHILD 0 NUMBER OF CHILDREN: '.$xml->getChildrenCount()); print('</pre>'); print('<pre>'); print('CHILD 0, CHILD 0 CHILDREN: '.$xml->image[0]->getChildrenCount()); print('</pre>'); $arrCompiled = $xml->image[0]->getChildrenValues(); print('<pre>'); print('CHILD 0, CHILD 0 NODE NAME / VALUE:<br>'); print(var_dump($arrCompiled)); //print($arrCompiled['name']); can also access by associative array name print('</pre>'); ?> Wondering if someone out there has any improvements for creating attribute checking within mixed nodes/elements/attributes such as: image id attribute with say 4 sub elements containing name, file etc. Peace. ---- Server IP: 69.147.83.197 Probable Submitter: 76.183.228.184 ---- Manual Page -- http://www.php.net/manual/en/function.simplexml-element-children.php Edit -- https://master.php.net/note/edit/73813 Del: integrated -- https://master.php.net/note/delete/73813/integrated Del: useless -- https://master.php.net/note/delete/73813/useless Del: bad code -- https://master.php.net/note/delete/73813/bad+code Del: spam -- https://master.php.net/note/delete/73813/spam Del: non-english -- https://master.php.net/note/delete/73813/non-english Del: in docs -- https://master.php.net/note/delete/73813/in+docs Del: other reasons-- https://master.php.net/note/delete/73813 Reject -- https://master.php.net/note/reject/73813 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124348) next »