xref: /PHP-5.3/ext/simplexml/tests/027.phpt (revision 610c7fbe)
1--TEST--
2SimpleXML: Adding an elements
3--SKIPIF--
4<?php if (!extension_loaded("simplexml")) print "skip"; ?>
5--FILE--
6<?php
7$xml =<<<EOF
8<people></people>
9EOF;
10
11function traverse_xml($xml, $pad = '')
12{
13  $name = $xml->getName();
14  echo "$pad<$name";
15  foreach($xml->attributes() as $attr => $value)
16  {
17    echo " $attr=\"$value\"";
18  }
19  echo ">" . trim($xml) . "\n";
20  foreach($xml->children() as $node)
21  {
22    traverse_xml($node, $pad.'  ');
23  }
24  echo $pad."</$name>\n";
25}
26
27
28$people = simplexml_load_string($xml);
29traverse_xml($people);
30$people->person = 'Joe';
31$people->person['gender'] = 'male';
32traverse_xml($people);
33$people->person = 'Jane';
34traverse_xml($people);
35$people->person['gender'] = 'female';
36$people->person[1] = 'Joe';
37$people->person[1]['gender'] = 'male';
38traverse_xml($people);
39$people->person[3] = 'Minni-me';
40$people->person[2]['gender'] = 'male';
41traverse_xml($people);
42$people->person[3]['gender'] = 'error';
43traverse_xml($people);
44?>
45===DONE===
46--EXPECTF--
47<people>
48</people>
49<people>
50  <person gender="male">Joe
51  </person>
52</people>
53<people>
54  <person gender="male">Jane
55  </person>
56</people>
57<people>
58  <person gender="female">Jane
59  </person>
60  <person gender="male">Joe
61  </person>
62</people>
63
64Warning: main(): Cannot add element person number 3 when only 2 such elements exist in %s027.php on line %d
65<people>
66  <person gender="female">Jane
67  </person>
68  <person gender="male">Joe
69  </person>
70  <person gender="male">Minni-me
71  </person>
72</people>
73<people>
74  <person gender="female">Jane
75  </person>
76  <person gender="male">Joe
77  </person>
78  <person gender="male">Minni-me
79  </person>
80  <person gender="error">
81  </person>
82</people>
83===DONE===
84