note 75311 added to function.simplexml-element-children
| From: | aero at osu1 dot php dot net | Date: | Wed, 23 May 2007 15:07:08 +0000 |
| Subject: | note 75311 added to function.simplexml-element-children | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126844@lists.php.net to get a copy of this message | ||
Here's a simple, recursive, function to transform XML data into pseudo E4X syntax ie.
root.child.value = foobar
<?php
error_reporting(E_ALL);
$xml = new SimpleXMLElement(
'<Patriarch>
<name>Bill</name>
<wife>
<name>Vi</name>
</wife>
<son>
<name>Bill</name>
</son>
<daughter>
<name>Jeri</name>
<husband>
<name>Mark</name>
</husband>
<son>
<name>Greg</name>
</son>
<son>
<name>Tim</name>
</son>
<son>
<name>Mark</name>
</son>
<son>
<name>Josh</name>
<wife>
<name>Kristine</name>
</wife>
<son>
<name>Blake</name>
</son>
<daughter>
<name>Liah</name>
</daughter>
</son>
</daughter>
</Patriarch>');
RecurseXML($xml);
function RecurseXML($xml,$parent="")
{
$child_count = 0;
foreach($xml as $key=>$value)
{
$child_count++;
if(RecurseXML($value,$parent.".".$key) == 0) // no childern, aka "leaf
node"
{
print($parent . "." . (string)$key . " = " . (string)$value .
"<BR>\n");
}
}
return $child_count;
}
?>
The output....
.name = Bill
.wife.name = Vi
.son.name = Bill
.daughter.name = Jeri
.daughter.husband.name = Mark
.daughter.son.name = Greg
.daughter.son.name = Tim
.daughter.son.name = Mark
.daughter.son.name = Josh
.daughter.son.wife.name = Kristine
.daughter.son.son.name = Blake
.daughter.son.daughter.name = Liah
----
Server IP: 64.71.164.2
Probable Submitter: 66.240.57.253
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-children.php
Edit -- https://master.php.net/note/edit/75311
Del: integrated -- https://master.php.net/note/delete/75311/integrated
Del: useless -- https://master.php.net/note/delete/75311/useless
Del: bad code -- https://master.php.net/note/delete/75311/bad+code
Del: spam -- https://master.php.net/note/delete/75311/spam
Del: non-english -- https://master.php.net/note/delete/75311/non-english
Del: in docs -- https://master.php.net/note/delete/75311/in+docs
Del: other reasons-- https://master.php.net/note/delete/75311
Reject -- https://master.php.net/note/reject/75311
Search -- https://master.php.net/manage/user-notes.php