xref: /PHP-5.5/ext/simplexml/tests/017.phpt (revision 9e29f174)
1--TEST--
2SimpleXML: iteration through subnodes
3--SKIPIF--
4<?php if (!extension_loaded("simplexml")) print "skip"; ?>
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  $persons = 2;
38  for ($i=0;$i<$persons;$i++) {
39    echo "person: ".$xml->person[$i]['name']."\n";
40	$children = 2;
41    for ($j=0;$j<$children;$j++) {
42      echo "  child: ".$xml->person[$i]->child[$j]['name']."\n";
43    }
44  }
45}
46
47echo "---11---\n";
48print_xml(simplexml_load_string($xml));
49echo "---12---\n";
50print_xml(simplexml_load_string($xml1));
51echo "---21---\n";
52print_xml2(simplexml_load_string($xml));
53echo "---22---\n";
54print_xml2(simplexml_load_string($xml1));
55?>
56===DONE===
57--EXPECTF--
58---11---
59person: Joe
60  child: Ann
61  child: Marray
62person: Boe
63  child: Joe
64  child: Ann
65---12---
66person: Joe
67  child: Ann
68---21---
69person: Joe
70  child: Ann
71  child: Marray
72person: Boe
73  child: Joe
74  child: Ann
75---22---
76person: Joe
77  child: Ann
78  child:
79person:
80
81Notice: Trying to get property of non-object in %s017.php on line %d
82  child:
83
84Notice: Trying to get property of non-object in %s017.php on line %d
85  child:
86===DONE===
87