XML Dateien einlesen vom Wechselrichter

Hallo,

ich habe mal eine Frage, wie lese ich am besten XML Dateien ein von meinem Solarworld Sunplug eco Wechselreichter?
Habt Ihr vielleicht einen Ansatz ohne den WWW Reader / Text Parser?

Die eine XML (aktuelle Leistungswerte) hat den folgenden Aufbau:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<Device Name="Sunplug eco 3.6 TL1i" Type="Inverter" Serial="752539AI005727******" BusAddress="2" NetBiosName="INV005727******" IpAddress="172.16.16.40" DateTime="2016-08-03T09:43:26">
<Measurements>
<Measurement Value="238.414" Unit="V" Type="AC_Voltage"/>
<Measurement Value="1.678" Unit="A" Type="AC_Current"/>
<Measurement Value="381.870" Unit="W" Type="AC_Power"/>
<Measurement Value="49.988" Unit="Hz" Type="AC_Frequency"/>
<Measurement Value="442.375" Unit="V" Type="DC_Voltage"/>
<Measurement Value="0.874" Unit="A" Type="DC_Current"/>
<Measurement Value="387.290" Unit="W" Type="DC_Power"/>
<Measurement Value="29.000" Unit="°C" Type="Temp"/>
<Measurement Value="381.9" Unit="W" Type="Consumption PV"/>
<Measurement Value="228.5" Unit="W" Type="Consumption Grid"/>
<Measurement Value="-228.450562" Unit="W" Type="GridPower"/>
<Measurement Value="100.0" Unit="%" Type="Dearting"/>
</Measurements>
</Device>
</root>

Schönen Gruß

Hallo yodaeichen,

vll hilft dir das ja weiter :wink:



<?

//Deine XML Datei 
$xmlstr ='
<root>
<Device Name="Sunplug eco 3.6 TL1i" Type="Inverter" Serial="752539AI005727******" BusAddress="2" NetBiosName="INV005727******" IpAddress="172.16.16.40" DateTime="2016-08-03T09:43:26">
<Measurements>
<Measurement Value="238.414" Unit="V" Type="AC_Voltage"/>
<Measurement Value="1.678" Unit="A" Type="AC_Current"/>
<Measurement Value="381.870" Unit="W" Type="AC_Power"/>
<Measurement Value="49.988" Unit="Hz" Type="AC_Frequency"/>
<Measurement Value="442.375" Unit="V" Type="DC_Voltage"/>
<Measurement Value="0.874" Unit="A" Type="DC_Current"/>
<Measurement Value="387.290" Unit="W" Type="DC_Power"/>
<Measurement Value="29.000" Unit="°C" Type="Temp"/>
<Measurement Value="381.9" Unit="W" Type="Consumption PV"/>
<Measurement Value="228.5" Unit="W" Type="Consumption Grid"/>
<Measurement Value="-228.450562" Unit="W" Type="GridPower"/>
<Measurement Value="100.0" Unit="%" Type="Dearting"/>
</Measurements>
</Device>
</root> '
;

print_r(json_decode(XML2JSON($xmlstr)));

//Funktion gefunden auf php.net
function XML2JSON($xml) {

        function normalizeSimpleXML($obj, &$result) {
            $data = $obj;
            if (is_object($data)) {
                $data = get_object_vars($data);
            }
            if (is_array($data)) {
                foreach ($data as $key => $value) {
                    $res = null;
                    normalizeSimpleXML($value, $res);
                    if (($key == '@attributes') && ($key)) {
                        $result = $res;
                    } else {
                        $result[$key] = $res;
                    }
                }
            } else {
                $result = $data;
            }
        }
        normalizeSimpleXML(simplexml_load_string($xml), $result);
        return json_encode($result);
    }
?>

weitere Infos findest du hier http://php.net/manual/de/class.simplexmlelement.php

Gruß

Wenn du nur einen Wert haben möchtest z.B. die AC Spannung gehts es wie folgt


$daten= json_decode(XML2JSON($xmlstr));
//print_r($daten);
echo $daten->Device->Measurements->Measurement[0]->Value." "
	 .$daten->Device->Measurements->Measurement[0]->Unit." "
	 .$daten->Device->Measurements->Measurement[0]->Type;