note 68276 added to ref.classobj

From: Date: Thu, 20 Jul 2006 13:18:13 +0000
Subject: note 68276 added to ref.classobj
Groups: php.notes 
Request: Send a blank email to php-notes+get-114973@lists.php.net to get a copy of this message
function from Tobias K..... stdClass Object to XML xml: [CODE] "$xml<?xml version='1.0' standalone='yes'?>" . "<movies>" . "<movie eur=\"10.00\">" . "<title>PHP: Behind the Parser</title>" . "<characters>" . "<character>" . "<name>Ms. Coder</name>" . "<actor>Onlivia Actora</actor>" . "</character>" . "<character>" . "<name>Mr. Coder</name>" . "<actor>El ActÓr</actor>" . "</character>" . "</characters>" . "<plot>" . "Text bla bla." . "</plot>" . "<rating ulf=\"integer\">7</rating>" . "<rating type=\"stars\">5</rating>" . "</movie>" . "</movies>"; [/CODE] stdObject: [CODE] stdClass Object ( [movie] => stdClass Object ( [title] => PHP: Behind the Parser [characters] => stdClass Object ( [character] => Array ( [0] => stdClass Object ( [name] => Ms. Coder [actor] => Onlivia Actora ) [1] => stdClass Object ( [name] => Mr. Coder [actor] => El ActÓr ) ) ) [plot] => Text bla bla. [rating] => Array ( [0] => stdClass Object ( [_] => 7 [ulf] => integer ) [1] => stdClass Object ( [_] => 5 [type] => stars ) ) [eur] => 10 ) ) [/CODE] Aufruf: [PHP] $s = htmlentities(asXML($r,"movies",array( "movies/movie" => "eur", "movies/movie/rating" => "ulf type" ))); [/PHP] Funktion: [PHP] function asXML($obj, $name, $attrNames=NULL, &$xml="", $path="", $depth=0) { if (is_array($obj)) { foreach ($obj as $v) asXML($v,$name,$attrNames,$xml,$path,$depth); } else { if ($path) $xml .= "\n"; $path = $path ? "$path/$name" : $name; $indent = str_repeat(" ",$depth*4); $xml .= "$indent<$name"; $attr = $attrNames[$path]; if ($attr) { if (!is_object($obj)) throw new Exception("asXML(): ". "$path is no object, so it cannot hold attributes"); $attr = explode(" ",$attr); foreach ($attr as $key) if ($obj->$key) $xml .= " $key=\"{$obj->$key}\""; } $xml .= ">"; if (is_object($obj)) { $xml .= $obj->_; $len = strlen($xml); foreach (get_object_vars($obj) as $key => $value) if ($key!="_" && (!is_array($attr) || !in_array($key,$attr))) asXML($value,$key,$attrNames,$xml,$path,$depth+1); if ($len!=strlen($xml)) $xml .= "\n$indent"; } else { $xml .= $obj; } $xml .= "</$name>"; } return $xml; } [/PHP] ---- Server IP: 217.160.72.57 Probable Submitter: 194.25.149.48 (proxied: unknown) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/ref.classobj.php Edit -- http://master.php.net/note/edit/68276 Del: integrated -- http://master.php.net/note/delete/68276/integrated Del: useless -- http://master.php.net/note/delete/68276/useless Del: bad code -- http://master.php.net/note/delete/68276/bad+code Del: spam -- http://master.php.net/note/delete/68276/spam Del: non-english -- http://master.php.net/note/delete/68276/non-english Del: in docs -- http://master.php.net/note/delete/68276/in+docs Del: other reasons-- http://master.php.net/note/delete/68276 Reject -- http://master.php.net/note/reject/68276 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114973) next »