PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP Grundlagen (http://www.selfphp.de/forum/forumdisplay.php?f=12)
-   -   stdClass to XML (http://www.selfphp.de/forum/showthread.php?t=14032)

Partheeus 20.07.2006 10:17:15

stdClass to XML
 
Moinsen...

hab jetzt schon alles durchsucht, versucht und ausprobiert. Aber zu keinem Ergebnis gekommen. :-(
Ich möchte gern eine function die mir erlaubt ein stdClass-Object in einen xml-String umzuwandeln.
Hat da jemand eine Idee bzw ein Beispiel wäre nicht schlecht um mir das mal vor Augen halten zu können. Hab grad völlig die Blockade. :-(

Schonmal vielen Dank.....

Partheeus 20.07.2006 15:05:11

AW: stdClass to XML
 
So, Tobias hats geschafft. :-)

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>";

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
        )

)

Aufruf:
PHP-Code:

$s htmlentities(asXML($r,"movies",array(
    
"movies/movie" => "eur",
    
"movies/movie/rating" => "ulf type"
))); 

Funktion:
PHP-Code:

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;




Alle Zeitangaben in WEZ +2. Es ist jetzt 10:53:13 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.