Browse Source
two new methods were added
two new methods were added
$node->count($subnode_name) - returns count of subnodes with specified name $node->attributes() - returns array of attributesPEAR_1_4DEV
3 changed files with 206 additions and 0 deletions
@ -0,0 +1,76 @@ |
|||||
|
--TEST-- |
||||
|
SimpleXML: iteration through subnodes |
||||
|
--SKIPIF-- |
||||
|
<?php if (!extension_loaded("simplexml")) print "skip"; ?> |
||||
|
--FILE-- |
||||
|
<?php |
||||
|
$xml =<<<EOF |
||||
|
<people> |
||||
|
<person name="Joe"> |
||||
|
<child name="Ann" /> |
||||
|
<child name="Marray" /> |
||||
|
</person> |
||||
|
<person name="Boe"> |
||||
|
<child name="Joe" /> |
||||
|
<child name="Ann" /> |
||||
|
</person> |
||||
|
</people> |
||||
|
EOF; |
||||
|
$xml1 =<<<EOF |
||||
|
<people> |
||||
|
<person name="Joe"> |
||||
|
<child name="Ann" /> |
||||
|
</person> |
||||
|
</people> |
||||
|
EOF; |
||||
|
|
||||
|
function print_xml($xml) { |
||||
|
foreach($xml as $person) { |
||||
|
echo "person: ".$person['name']."\n"; |
||||
|
foreach($person as $child) { |
||||
|
echo " child: ".$child['name']."\n"; |
||||
|
} |
||||
|
} |
||||
|
echo "----------\n"; |
||||
|
} |
||||
|
|
||||
|
function print_xml2($xml) { |
||||
|
$persons = $xml->count("person"); |
||||
|
for ($i=0;$i<$persons;$i++) { |
||||
|
echo "person: ".$xml->person[$i]['name']."\n"; |
||||
|
$children = $xml->person[$i]->count("child"); |
||||
|
for ($j=0;$j<$children;$j++) { |
||||
|
echo " child: ".$xml->person[$i]->child[$j]['name']."\n"; |
||||
|
} |
||||
|
} |
||||
|
echo "----------\n"; |
||||
|
} |
||||
|
|
||||
|
print_xml(simplexml_load_string($xml)); |
||||
|
print_xml(simplexml_load_string($xml1)); |
||||
|
print_xml2(simplexml_load_string($xml)); |
||||
|
print_xml2(simplexml_load_string($xml1)); |
||||
|
echo "---Done---\n"; |
||||
|
?> |
||||
|
--EXPECT-- |
||||
|
person: Joe |
||||
|
child: Ann |
||||
|
child: Marray |
||||
|
person: Boe |
||||
|
child: Joe |
||||
|
child: Ann |
||||
|
---------- |
||||
|
person: Joe |
||||
|
child: Ann |
||||
|
---------- |
||||
|
person: Joe |
||||
|
child: Ann |
||||
|
child: Marray |
||||
|
person: Boe |
||||
|
child: Joe |
||||
|
child: Ann |
||||
|
---------- |
||||
|
person: Joe |
||||
|
child: Ann |
||||
|
---------- |
||||
|
---Done--- |
||||
@ -0,0 +1,65 @@ |
|||||
|
--TEST-- |
||||
|
SimpleXML: iteration through subnodes and attributes |
||||
|
--SKIPIF-- |
||||
|
<?php if (!extension_loaded("simplexml")) print "skip"; ?> |
||||
|
--FILE-- |
||||
|
<?php |
||||
|
$xml =<<<EOF |
||||
|
<people> |
||||
|
<person name="Joe"> |
||||
|
Text1 |
||||
|
<child name="Ann" /> |
||||
|
Text2 |
||||
|
<child name="Marray" /> |
||||
|
Text3 |
||||
|
</person> |
||||
|
<person name="Boe"> |
||||
|
<child name="Joe" /> |
||||
|
<child name="Ann" /> |
||||
|
</person> |
||||
|
</people> |
||||
|
EOF; |
||||
|
$xml1 =<<<EOF |
||||
|
<people> |
||||
|
<person name="Joe"> |
||||
|
<child /> |
||||
|
</person> |
||||
|
</people> |
||||
|
EOF; |
||||
|
|
||||
|
function traverse_xml($pad,$xml) { |
||||
|
foreach($xml as $name => $node) { |
||||
|
echo $pad."<$name"; |
||||
|
foreach($node->attributes() as $attr => $value) { |
||||
|
echo " $attr=\"$value\""; |
||||
|
} |
||||
|
echo ">\n"; |
||||
|
traverse_xml($pad." ",$node); |
||||
|
echo $pad."</$name>\n"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
traverse_xml("",simplexml_load_string($xml)); |
||||
|
echo "----------\n"; |
||||
|
traverse_xml("",simplexml_load_string($xml1)); |
||||
|
echo "---Done---\n"; |
||||
|
?> |
||||
|
--EXPECT-- |
||||
|
<person name="Joe"> |
||||
|
<child name="Ann"> |
||||
|
</child> |
||||
|
<child name="Marray"> |
||||
|
</child> |
||||
|
</person> |
||||
|
<person name="Boe"> |
||||
|
<child name="Joe"> |
||||
|
</child> |
||||
|
<child name="Ann"> |
||||
|
</child> |
||||
|
</person> |
||||
|
---------- |
||||
|
<person name="Joe"> |
||||
|
<child> |
||||
|
</child> |
||||
|
</person> |
||||
|
---Done--- |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue