note 103816 added to book.simplexml
| From: | streaver91 at gmail dot com | Date: | Thu, 05 May 2011 03:49:20 +0000 |
| Subject: | note 103816 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179439@lists.php.net to get a copy of this message | ||
The BIGGEST differece between an XML and a PHP array is that in an XML file, the name of elements
can be the same even if they are siblings, eg. "<pa><ch /><ch /><ch
/></pa>", while in an PHP array, the key of which must be different.
I think the array structure developed by svdmeer can fit for XML, and fits well.
here is an example array converted from an xml file:
array(
"@tag"=>"name",
"@attr"=>array(
"id"=>"1","class"=>"2")
"@text"=>"some text",
)
or if it has childrens, that can be:
array(
"@tag"=>"name",
"@attr"=>array(
"id"=>"1","class"=>"2")
"@items"=>array(
0=>array(
"@tag"=>"name","@text"=>"some text"
)
)
Also, I wrote a function that can change that array back to XML.
function array2XML($arr,$root) {
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\"
?><{$root}></{$root}>");
$f = create_function('$f,$c,$a','
foreach($a as $v) {
if(isset($v["@text"])) {
$ch = $c->addChild($v["@tag"],$v["@text"]);
} else {
$ch = $c->addChild($v["@tag"]);
if(isset($v["@items"])) {
$f($f,$ch,$v["@items"]);
}
}
if(isset($v["@attr"])) {
foreach($v["@attr"] as $attr => $val) {
$ch->addAttribute($attr,$val);
}
}
}');
$f($f,$xml,$arr);
return $xml->asXML();
}
----
Server IP: 69.147.83.197
Probable Submitter: 59.78.22.147
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/103816
Del: integrated -- https://master.php.net/note/delete/103816/integrated
Del: useless -- https://master.php.net/note/delete/103816/useless
Del: bad code -- https://master.php.net/note/delete/103816/bad+code
Del: spam -- https://master.php.net/note/delete/103816/spam
Del: non-english -- https://master.php.net/note/delete/103816/non-english
Del: in docs -- https://master.php.net/note/delete/103816/in+docs
Del: other reasons-- https://master.php.net/note/delete/103816
Reject -- https://master.php.net/note/reject/103816
Search -- https://master.php.net/manage/user-notes.php