note 73813 deleted from function.simplexml-element-children by mazzanet
| From: | mazzanet@php.net | Date: | Mon, 12 Mar 2007 07:45:23 +0000 |
| Subject: | note 73813 deleted from function.simplexml-element-children by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-124363@lists.php.net to get a copy of this message | ||
Note Submitter: studio at dfwstudios dot com
----
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.