IPS 2.5 -> Hilfe! Script liefert ein anderes Ergebnis

Das unten stehende Script liefert in IPS 2.5 Beta ein anderes Ergebnis zurück als unter 2.4. Ich suche jetzt schon zwei Tage nach der Ursache , aber ich habe null Ideen. Hat jemand einen Tip ?

Einige Array Werte sind unter 2.5 leer, in 2.4 sind dort Werte eingetragen.

//--- XML 2 Array (php.net Copy&Paste)
function convert_xml2array(&$result,$root,$rootname='root'){

    $n=count($root->children());

    if ($n>0){

        /**
         * start of the correction
         */
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
            foreach ($root->attributes() as $atr=>$value){
                $result[$rootname]['@attributes'][$atr]=(string)$value;
            }
        }
        /**
         *  end of the correction
         */

         foreach ($root->children() as $child){
             $name=$child->getName();
             convert_xml2array($result[$rootname][],$child,$name);
         }
    } else {
        $result[$rootname]= (array) $root;
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
        }
    }
}

Gruß Sven

&$result kannst Du seit PHP5.4 nicht mehr in der functions übergeben.

Mach die $result in der function GLOBAL zur Verfügung

function convert_xml2array($root,$rootname='root'){

GLOBAL $result;

$n=count($root->children());

Habe es zwar auch noch nicht 100%ig verstanden aber so funktionieren meine Funktionen.

Hier gibt es noch einen Beitrag dazu.

http://www.ip-symcon.de/forum/f64/call-time-pass-reference-16157/

Der Beitrag von Brownson (verlinkte Threads) liest sich für mich so, dass Dein Code der Funktion schon richtig ist, aber der Funktionsaufruf zusätzlich ohne das „&“ erfolgen müsste… !?

… versuch mal das, bin mir aber auch nicht sicher…


//--- XML 2 Array (php.net Copy&Paste)
function convert_xml2array(&$result,$root,$rootname='root'){
    $n=count($root->children());
    if ($n>0){
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
            foreach ($root->attributes() as $atr=>$value){
                $result[$rootname]['@attributes'][$atr]=(string)$value;
            }
        }
         foreach ($root->children() as $child){
             $name=$child->getName();
             $childResult = array();
             convert_xml2array($childResult,$child,$name);
             $result[$rootname][] = $childResult;
         }
    } else {
        $result[$rootname]= (array) $root;
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
        }
    }
}

Erstmal danke für die schnellen Antworten. Aber leider hat bis jetzt nichts geholfen.

@wgreipl - deinen Link kann ich leider nicht öffnen !

@bengie - ohne das „&“ gibt es immer eine Fehlermeldung

@Brownson - das Ergebnis deiner Änderungen sind identisch mit dem original Script.

Generell liefert das Script ja Daten zurück, nur leider sind einzelne Werte leer. Ich bin extra noch mal auf IPS2.4 zurückgegangen um mir das Ergebnis dort anzuschauen. Dort sind alle Werte vorhanden.

Gruß Sven

Kannst Du da mal ein konkretes Beispiel posten (Input, Output falsch, Output richtig)

Aufruf der Funktion

 $result=array();
 $doc=simplexml_load_string($content);
 convert_xml2array($result, $doc);

das steht in der Quell Variablen ($doc)

SimpleXMLElement Object
(
    [device] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [class] => 24070
                    [UID] => -1609562152
                    [type] => 36414PB        
                    [name] => Backofen
                    [state] => 2
                    [additionalName] => SimpleXMLElement Object
                        (
                        )

                    [room] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [id] => null
                                    [level] => null
                                )

                        )

                    [information] => SimpleXMLElement Object
                        (
                            [key] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => Gerätestatus
                                            [value] => Bereit
                                        )

                                )

                        )

                    [actions] => SimpleXMLElement Object
                        (
                            [action] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => Details
                                             => http://xxx.xxx.xxx.xxx:80/homebus/device?language=de_DE&type=OV_H4000&id=OV_H4000.-1609562152
                                        )

                                )

                        )

                )

das ist unter 2.5 das Ergebnis ($result)

Array
(
    [root] => Array
        (
            [@attributes] => Array
                (
                )

            [0] => Array
                (
                    [device] => Array
                        (
                            [@attributes] => Array
                                (
                                )

                            [0] => Array
                                (
                                    [class] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [UID] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [type] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [name] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [4] => Array
                                (
                                    [state] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [5] => Array
                                (
                                    [additionalName] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [6] => Array
                                (
                                    [room] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [id] => null
                                                    [level] => null
                                                )

                                        )

                                )

                            [7] => Array
                                (
                                    [information] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [key] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [name] => Gerätestatus
                                                                    [value] => Bereit
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [8] => Array
                                (
                                    [actions] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [action] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [name] => Details
                                                                     => http://xxx.xxx.xxx.xxx:80/homebus/device?language=de_DE&type=OV_H4000&id=OV_H4000.-1609562152
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

das ist das Ergebnis unter 2.4 ($result)

Array
(
    [root] => Array
        (
            [@attributes] => Array
                (
                )

            [0] => Array
                (
                    [device] => Array
                        (
                            [@attributes] => Array
                                (
                                )

                            [0] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => 24070
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [UID] => Array
                                        (
                                            [0] => -1609562152
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [type] => Array
                                        (
                                            [0] => 36414PB        
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [name] => Array
                                        (
                                            [0] => Backofen
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [4] => Array
                                (
                                    [state] => Array
                                        (
                                            [0] => 2
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [5] => Array
                                (
                                    [additionalName] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [6] => Array
                                (
                                    [room] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [id] => null
                                                    [level] => null
                                                )

                                        )

                                )

                            [7] => Array
                                (
                                    [information] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [key] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [name] => Gerätestatus
                                                                    [value] => Bereit
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [8] => Array
                                (
                                    [actions] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [action] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [name] => Details
                                                                     => http://xxx.xxx.xxx.xxx:80/homebus/device?language=de_DE&type=OV_H4000&id=OV_H4000.-1609562152
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

unter 2.5 sind die ersten Einträge in Array alle leer .

z.B.


                            [3] => Array
                                (
                                    [name] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                ) 

Hallo,

Das Problem liegt an folgender Zeile:


   $result[$rootname]= (array)$root;

hier wird der eigentliche Wert des XML Knotens nicht richtig in ein Array umgewandelt. Wie eine korrekte Umwandlung funktionert bin ich mir auch nicht sicher (XML ist nicht wirklich mein Gebiet), aber folgender Code scheint zu funktionieren:

   
   $result[$rootname]= (array)(string)$root;

oder

   
   $result[$rootname]= array("$root");


function convert_xml2array(&$result,$root,$rootname='root'){

    $n=count($root->children());

    if ($n>0){

        /**
         * start of the correction
         */
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
            foreach ($root->attributes() as $atr=>$value){
                $result[$rootname]['@attributes'][$atr]=(string)$value;
            }
        }
        /**
         *  end of the correction
         */

         foreach ($root->children() as $child){
             $name=$child->getName();
             convert_xml2array($result[$rootname][],$child,$name);
         }
    } else {
        $result[$rootname]= (array)(string)$root;
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
        }
    }
}

so habe es gleich mal getestet.

Jetzt sind die ersten Einträge okay, aber die letzten nicht mehr.

Array
(
    [root] => Array
        (
            [@attributes] => Array
                (
                )

            [0] => Array
                (
                    [device] => Array
                        (
                            [@attributes] => Array
                                (
                                )

                            [0] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => 24070
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [UID] => Array
                                        (
                                            [0] => -1609562152
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [2] => Array
                                (
                                    [type] => Array
                                        (
                                            [0] => 36414PB        
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [name] => Array
                                        (
                                            [0] => Backofen
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [4] => Array
                                (
                                    [state] => Array
                                        (
                                            [0] => 2
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [5] => Array
                                (
                                    [additionalName] => Array
                                        (
                                            [0] => 
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [6] => Array
                                (
                                    [room] => Array
                                        (
                                            [0] => 
                                            [@attributes] => Array
                                                (
                                                )

                                        )

                                )

                            [7] => Array
                                (
                                    [information] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [key] => Array
                                                        (
                                                            [0] => 
                                                            [@attributes] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [8] => Array
                                (
                                    [actions] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                )

                                            [0] => Array
                                                (
                                                    [action] => Array
                                                        (
                                                            [0] => 
                                                            [@attributes] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

Die Einträge für die Felder 7 u. 8 sind jetzt leer.

danke u. Gruß Sven

Kannst Du mal das ursprüngliche XML (Variable $content) nochmals posten…

<DEVICES><device><class>24070</class><UID>-1609562152</UID><type>36414PB        </type><name>Backofen</name><state>2</state><additionalName/><room id="null" level="null"/><information>  <key name="Gerätestatus" value="Bereit"/></information><actions>  <action name="Details" URL="http://xxx.xxx.xxx.xxx:80/homebus/device?language=de_DE&type=OV_H4000&id=OV_H4000.-1609562152"/></actions></device>

das steht drinnen stehen.

Sven

Ich bilde mir jetzt ein das selbe Ergebnis zu haben - weiß aber noch nicht wieso, insbesondere nicht was sich in PHP geändert hat??

Probiers mal …


function convert_xml2array(&$result,$root,$rootname='root'){
    $n=count($root->children());
    if ($n>0){
        if (!isset($result[$rootname]['@attributes'])){
            $result[$rootname]['@attributes']=array();
            foreach ($root->attributes() as $atr=>$value){
                $result[$rootname]['@attributes'][$atr]=(string)$value;
            }
        }
         foreach ($root->children() as $child){
             $name=$child->getName();
             convert_xml2array($result[$rootname][],$child,$name);
         }
    } else {
        	if ("$root" <> null) $result[$rootname]= array("$root");
         $result[$rootname]['@attributes']=array();
         foreach ($root->attributes() as $atr=>$value){
         	$result[$rootname]['@attributes'][$atr]=(string)$value;
         }
    }
}

Ich habe die blöde Funktion einfach weggelassen. Das komplette Skript, dass Sven eigentlich sucht, gibt es hier: http://www.ip-symcon.de/forum/f53/miele-home-einbinden-16403/#post142130 Dort gibt es auch die neue IPS_SetIdent Funktion zu bestauenen :smiley:

paresy

Hallo Brownson,

das letzte Script läuft ohne Probleme. Nochmal besten Dank für die Hilfe und hoffentlich viele Weihnachtsgeschenke.

Gruß Sven