SimpleXMLElement->children

(no version information, might be only in CVS)

SimpleXMLElement->children --  Finds children of given node

Description

SimpleXMLElement simplexml_element->children ( [string nsprefix] )

This method finds the children of the element of which it is a member. The result follows normal iteration rules.

Замечание: SimpleXML содержит правило добавления итеративных свойств к большинству методов. Они не могут быть просмотрены с испольванием var_dump() или каких-либо других средств анализа объектов.

Пример 1. Traversing a children() pseudo-array

<?php
$xml
= simplexml_load_string(
'<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'];
       }
   }
}
?>

This script 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


SimpleXMLElement->children
taylorbarstow at that google mail thingy
21-Apr-2006 07:38
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
27-Oct-2005 03:45
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.
no-one
31-May-2005 06:05
For anyone who hasn't read Sterling Hughe's article (http://www.zend.com/php5/articles/php5-simplexml.php):

<?php
$xml_document
=<<<EOT
<?xml version="1.0"?>
<root xmlns:foo="http://google.com">
  <foo:bar>baz</foo:bar>
</root>
EOT;

$xml_document = simplexml_load_xml($xml_document);

$foo_ns_bar = $xml_document->children('http://google.ca');

echo
$foo_ns_bar->bar[0]; // prints 'baz'
?>
Andrew Rose (rose dot andrew at gmail dot com)
16-Mar-2005 04:48
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";

?>
zyxwvu at users dot sourceforge dot net
15-Feb-2004 10:32
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/>';
}

<SimpleXMLElement->attributesSimpleXMLElement->xpath>
 Last updated: Tue, 15 Nov 2005