note 103614 added to class.simplexmlelement
| From: | dans at dansheps dot com | Date: | Fri, 22 Apr 2011 19:06:16 +0000 |
| Subject: | note 103614 added to class.simplexmlelement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179088@lists.php.net to get a copy of this message | ||
I created a little function to parse the simple XML into a structure which is easier to iterate(I
believe anyways) than the simpleXML node structure.
<?php
function parseSimpleXML($xmldata)
{
$childNames = array();
$children = array();
if( $xmldata->count() !== 0 )
{
foreach( $xmldata->children() AS $child )
{
$name = $child->getName();
if( !isset($childNames[$name]) )
{
$childNames[$name] = 0;
}
$childNames[$name]++;
$children[$name][] = $this->parseSimpleXML($child);
}
}
$returndata = new XMLNode();
if( $xmldata->attributes()->count() > 0 )
{
$returndata->{'@attributes'} = new XMLAttribute();
foreach( $xmldata->attributes() AS $name => $attrib )
{
$returndata->{'@attributes'}->{$name} = (string)$attrib;
}
}
if( count($childNames) > 0 )
{
foreach( $childNames AS $name => $count )
{
if( $count === 1 )
{
$returndata->{$name} = $children[$name][0];
}
else
{
$returndata->{$name} = new XMLMultiNode();
$counter = 0;
foreach( $children[$name] AS $data )
{
$returndata->{$name}->{$counter} = $data;
$counter++;
}
}
}
}
else
{
if( (string)$xmldata !== '' )
{
$returndata->{'@innerXML'} = (string)$xmldata;
}
}
return $returndata;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 50.72.67.8
----
Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php
Edit -- https://master.php.net/note/edit/103614
Del: integrated -- https://master.php.net/note/delete/103614/integrated
Del: useless -- https://master.php.net/note/delete/103614/useless
Del: bad code -- https://master.php.net/note/delete/103614/bad+code
Del: spam -- https://master.php.net/note/delete/103614/spam
Del: non-english -- https://master.php.net/note/delete/103614/non-english
Del: in docs -- https://master.php.net/note/delete/103614/in+docs
Del: other reasons-- https://master.php.net/note/delete/103614
Reject -- https://master.php.net/note/reject/103614
Search -- https://master.php.net/manage/user-notes.php