note 105997 added to book.simplexml
| From: | salvocucinella at gmail dot com | Date: | Fri, 30 Sep 2011 20:58:20 +0000 |
| Subject: | note 105997 added to book.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183548@lists.php.net to get a copy of this message | ||
Here is a snippet to convert a simple object to XML.
A class that does the job and an example how to use it
<?php
class Obj2xml {
var $xmlResult;
function __construct($rootNode){
$this->xmlResult = new SimpleXMLElement("<$rootNode></$rootNode>");
}
private function iteratechildren($object,$xml){
foreach ($object as $name=>$value) {
if (is_string($value) || is_numeric($value)) {
$xml->$name=$value;
} else {
$xml->$name=null;
$this->iteratechildren($value,$xml->$name);
}
}
}
function toXml($object) {
$this->iteratechildren($object,$this->xmlResult);
return $this->xmlResult->asXML();
}
}
//----test object----
$ob->campo1=10;
$ob->campo2->campo21="campo21";
$ob->campo2->campo22="campo22";
//----test object----
$converter=new Obj2xml("root");
header("Content-Type:text/xml");
echo $converter->toXml($ob);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 95.236.35.177
----
Manual Page -- http://www.php.net/manual/en/book.simplexml.php
Edit -- https://master.php.net/note/edit/105997
Del: integrated -- https://master.php.net/note/delete/105997/integrated
Del: useless -- https://master.php.net/note/delete/105997/useless
Del: bad code -- https://master.php.net/note/delete/105997/bad+code
Del: spam -- https://master.php.net/note/delete/105997/spam
Del: non-english -- https://master.php.net/note/delete/105997/non-english
Del: in docs -- https://master.php.net/note/delete/105997/in+docs
Del: other reasons-- https://master.php.net/note/delete/105997
Reject -- https://master.php.net/note/reject/105997
Search -- https://master.php.net/manage/user-notes.php