PHP : Function Reference : SimpleXML functions : SimpleXMLElement->children()
Example 2252. Traversing a children() pseudo-array
<?php
$xml = new SimpleXMLElement( '<person>
<child role="son">
<child role="daughter"/>
</child>
<child role="daughter">
<child role="son">
<child role="son"/>
</child>
</child>
</person>');
foreach ($xml->children() as $second_gen) {
echo ' The person begot a ' . $second_gen['role'];
foreach ($second_gen->children() as $third_gen) {
echo ' who begot a ' . $third_gen['role'] . ';';
foreach ($third_gen->children() as $fourth_gen) {
echo ' and that ' . $third_gen['role'] .
' begot a ' . $fourth_gen['role'];
}
}
} ?>
The above example will output:
The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
andrew rose rose dot andrew
The example below shows the basic use of depth-first recursion to span the xml tree.
This is coded for the command line, and it prints out the original sentance above and then the copy cat sentence it creates itself for comparison, which as you will see; this example is slightly off from, I'll leave it upto you to resolve this issue.
All in all I personaly think xml and recursion go hand in hand, so if you don't understand recursion but know xml and want to use php to manipulate xml you will need to learn about recursion at some point.
<?
$xml = simplexml_load_string(
'<person>
<child role="son">
<child role="daughter"/>
</child>
<child role="daughter">
<child role="son">
<child role="son"/>
</child>
</child>
</person>');
function recurse($child)
{
foreach($child->children() as $children) {
echo ' who begot a '.$children['role'];
recurse($children);
}
return;
}
foreach($xml->children() as $children) {
echo 'The person begot a '.$children['role'];
recurse($children, 0);
echo '; ';
}
echo "\n";
echo 'The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son'."\n";
?>
taylorbarstow
Sometimes you actually want an array, not a pseudo array. This is especially true when you aren't dealing with attributes (i.e., you just want the array of child nodes).
Do like this:
<?php
$children = $sxml->xpath('child::node()');
?>
The reason you might want this is to be able to use array functions like array_shift, array_pop, etc. This is especially true when you are writing recursive functions. Simplexml works really well in iterative programming, but if you try to implement recursion it gets ugly.
sebastian
Just a quick addition:
If you need to access a child node which contains a dash, you need to encapsulate it with {""}.
For example:
<?php
foreach ($domain->domain-listing as $product) {
}
?>
The example above doesn't work because of the dash. But instead you need to use:
<?php
foreach ($domain->{"domain-listing"} as $product) {
}
?>
At least for me the second example works perfectly fine.
aero
Here's a slightly modified function that transforms xml data into an associative array with the indices's into the array have the following syntax, parent.child etc.
So for
<meal>
<type>Lunch</type>
<time>12:30</time>
<menu>
<entree>salad</entree>
<maincourse>steak</maincourse>
</menu>
</meal>
The array would look like this...
array(4) {
["type"]=>
string(5) "Lunch"
["time"]=>
string(5) "12:30"
["menu.entree"]=>
string(5) "salad"
["menu.maincourse"]=>
string(5) "steak"
}
Here's an example
<?php
$xml = new SimpleXMLElement(
'<meal>
<type>Lunch</type>
<time>12:30</time>
<menu>
<entree>salad</entree>
<maincourse>steak</maincourse>
</menu>
</meal>');
$vals = array();
RecurseXML($xml,$vals);
foreach($vals as $key=>$value)
print("{$key} = {$value} \n");
function RecurseXML($xml,&$vals,$parent="")
{
$child_count = 0;
foreach($xml as $key=>$value)
{
$child_count++;
$k = ($parent == "") ? (string)$key : $parent . "." . (string)$key;
if(RecurseXML($value,$vals,$k) == 0) // no childern, aka "leaf node"
$vals[$k] = (string)$value;
}
return $child_count;
}
The output:
type = Lunch
time = 12:30
menu.entree = salad
menu.maincourse = steak
?>
no-one
zyxwvu
File:
<category>
<item>text</item>
<bold>text</bold>
<item>text</item>
<item>text</item>
<mark>text</mark>
<bold>text</bold>
</category>
If you want to get also names of the tags, you can use this loop layout:
foreach($category -> children() as $name => $node){
echo $name.'<br/>';
}
|