note 70433 added to ref.simplexml
| From: | roy dot walter at nospam dot brookhouse dot co dot uk | Date: | Mon, 16 Oct 2006 16:15:08 +0000 |
| Subject: | note 70433 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-118578@lists.php.net to get a copy of this message | ||
simplexml provides a neat way to do 'ini' files. Preferences for any number of users can
be held in a single XML file having elements for each user name with user specific preferences as
attributes of child elements. The separate <pref/>'s could of course be combined as
multiple attributes of a single <pref/> element but this could get unwieldy.
In the sample code below the makeXML() function uses the simplexml_load_string function to generate
some XML to play with and the readPrefs() function parses the requested users preferences into an
array.
<?
function makeXML() {
$xmlString = <<<XML
<preferences>
<johndoe>
<pref color="#FFFFFF"/>
<pref size="14"/>
<pref font="Verdana"/>
</johndoe>
<janedoe>
<pref color="#000000"/>
<pref size="16"/>
<pref font="Georgia"/>
</janedoe>
</preferences>
XML;
return simplexml_load_string($xmlString);
}
function readPrefs($user, $xml) {
foreach($xml->$user as $arr);
$n = count($arr);
for($i=0;$i<$n;$i++) {
foreach($xml->$user->pref[$i]->attributes() as $a=>$b) {
$prefs[$a] = (string)$b;
}
}
print_r($prefs);
}
readPrefs('johndoe', makeXML());
?>
----
Server IP: 66.163.161.117
Probable Submitter: 81.137.237.103
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- https://master.php.net/note/edit/70433
Del: integrated -- https://master.php.net/note/delete/70433/integrated
Del: useless -- https://master.php.net/note/delete/70433/useless
Del: bad code -- https://master.php.net/note/delete/70433/bad+code
Del: spam -- https://master.php.net/note/delete/70433/spam
Del: non-english -- https://master.php.net/note/delete/70433/non-english
Del: in docs -- https://master.php.net/note/delete/70433/in+docs
Del: other reasons-- https://master.php.net/note/delete/70433
Reject -- https://master.php.net/note/reject/70433
Search -- https://master.php.net/manage/user-notes.php