xref: /PHP-8.2/ext/simplexml/tests/017.phpt (revision 7f2f0c00)
1--TEST--
2SimpleXML: iteration through subnodes
3--EXTENSIONS--
4simplexml
5--FILE--
6<?php
7$xml =<<<EOF
8<people>
9   <person name="Joe">
10     <child name="Ann" />
11     <child name="Marray" />
12   </person>
13   <person name="Boe">
14     <child name="Joe" />
15     <child name="Ann" />
16   </person>
17</people>
18EOF;
19$xml1 =<<<EOF
20<people>
21   <person name="Joe">
22     <child name="Ann" />
23   </person>
24</people>
25EOF;
26
27function print_xml($xml) {
28  foreach($xml->children() as $person) {
29    echo "person: ".$person['name']."\n";
30    foreach($person->children() as $child) {
31      echo "  child: ".$child['name']."\n";
32    }
33  }
34}
35
36function print_xml2($xml) {
37  for ($i=0;$i<count($xml->person);$i++) {
38    $person = $xml->person[$i];
39    echo "person: ".$person['name']."\n";
40    for ($j=0;$j<count($person->child);$j++) {
41      echo "  child: ".$person->child[$j]['name']."\n";
42    }
43  }
44}
45
46echo "---11---\n";
47print_xml(simplexml_load_string($xml));
48echo "---12---\n";
49print_xml(simplexml_load_string($xml1));
50echo "---21---\n";
51print_xml2(simplexml_load_string($xml));
52echo "---22---\n";
53print_xml2(simplexml_load_string($xml1));
54?>
55--EXPECT--
56---11---
57person: Joe
58  child: Ann
59  child: Marray
60person: Boe
61  child: Joe
62  child: Ann
63---12---
64person: Joe
65  child: Ann
66---21---
67person: Joe
68  child: Ann
69  child: Marray
70person: Boe
71  child: Joe
72  child: Ann
73---22---
74person: Joe
75  child: Ann
76