xref: /PHP-8.4/ext/simplexml/tests/018.phpt (revision 7f2f0c00)
1--TEST--
2SimpleXML: iteration through subnodes and attributes
3--EXTENSIONS--
4simplexml
5--FILE--
6<?php
7$xml =<<<EOF
8<people>
9   <person name="Joe">
10     Text1
11     <child name="Ann" />
12     Text2
13     <child name="Marray" />
14     Text3
15   </person>
16   <person name="Boe">
17     <child name="Joe" />
18     <child name="Ann" />
19   </person>
20</people>
21EOF;
22$xml1 =<<<EOF
23<people>
24   <person name="Joe">
25     <child />
26   </person>
27</people>
28EOF;
29
30function traverse_xml($pad,$xml) {
31  foreach($xml->children() as $name => $node) {
32    echo $pad."<$name";
33    foreach($node->attributes() as $attr => $value) {
34      echo " $attr=\"$value\"";
35    }
36    echo ">\n";
37    traverse_xml($pad."  ",$node);
38    echo $pad."</$name>\n";
39  }
40}
41
42traverse_xml("",simplexml_load_string($xml));
43echo "----------\n";
44traverse_xml("",simplexml_load_string($xml1));
45echo "---Done---\n";
46?>
47--EXPECT--
48<person name="Joe">
49  <child name="Ann">
50  </child>
51  <child name="Marray">
52  </child>
53</person>
54<person name="Boe">
55  <child name="Joe">
56  </child>
57  <child name="Ann">
58  </child>
59</person>
60----------
61<person name="Joe">
62  <child>
63  </child>
64</person>
65---Done---
66