1--TEST-- 2SimpleXML: Adding an elements 3--EXTENSIONS-- 4simplexml 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--EXPECTF-- 46<people> 47</people> 48<people> 49 <person gender="male">Joe 50 </person> 51</people> 52<people> 53 <person gender="male">Jane 54 </person> 55</people> 56<people> 57 <person gender="female">Jane 58 </person> 59 <person gender="male">Joe 60 </person> 61</people> 62 63Warning: main(): Cannot add element person number 3 when only 2 such elements exist in %s027.php on line %d 64<people> 65 <person gender="female">Jane 66 </person> 67 <person gender="male">Joe 68 </person> 69 <person gender="male">Minni-me 70 </person> 71</people> 72<people> 73 <person gender="female">Jane 74 </person> 75 <person gender="male">Joe 76 </person> 77 <person gender="male">Minni-me 78 </person> 79 <person gender="error"> 80 </person> 81</people> 82