xref: /PHP-8.3/ext/simplexml/tests/019.phpt (revision 7f2f0c00)
1--TEST--
2SimpleXML: foreach with children()
3--EXTENSIONS--
4simplexml
5--FILE--
6<?php
7
8$sxe = simplexml_load_string(<<<EOF
9<?xml version='1.0'?>
10<!DOCTYPE sxe SYSTEM "notfound.dtd">
11<sxe id="elem1">
12 Plain text.
13 <elem1 attr1='first'>
14  Bla bla 1.
15  <!-- comment -->
16  <elem2>
17   Here we have some text data.
18   <elem3>
19    And here some more.
20    <elem4>
21     Wow once again.
22    </elem4>
23   </elem3>
24  </elem2>
25 </elem1>
26 <elem11 attr2='second'>
27  Bla bla 2.
28  <elem111>
29   Foo Bar
30  </elem111>
31 </elem11>
32</sxe>
33EOF
34);
35
36foreach($sxe->children() as $name => $data) {
37    var_dump($name);
38    var_dump(trim($data));
39}
40
41echo "===CLONE===\n";
42
43foreach(clone $sxe->children() as $name => $data) {
44    var_dump($name);
45    var_dump(trim($data));
46}
47
48echo "===ELEMENT===\n";
49
50foreach($sxe->elem11->children() as $name => $data) {
51    var_dump($name);
52    var_dump(trim($data));
53}
54
55echo "===COMMENT===\n";
56
57foreach($sxe->elem1->children() as $name => $data) {
58    var_dump($name);
59    var_dump(trim($data));
60}
61
62?>
63--EXPECT--
64string(5) "elem1"
65string(10) "Bla bla 1."
66string(6) "elem11"
67string(10) "Bla bla 2."
68===CLONE===
69string(5) "elem1"
70string(10) "Bla bla 1."
71string(6) "elem11"
72string(10) "Bla bla 2."
73===ELEMENT===
74string(7) "elem111"
75string(7) "Foo Bar"
76===COMMENT===
77string(5) "elem2"
78string(28) "Here we have some text data."
79