PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   PHP Grundlagen (http://www.selfphp.de/forum/forumdisplay.php?f=12)
-   -   XML Datei auslesen und in Variable speichern (http://www.selfphp.de/forum/showthread.php?t=25737)

wildsau 16.12.2015 23:35:44

XML Datei auslesen und in Variable speichern
 
Hallo

ich habe eine XML Datei aus der ich Daten auslesen möchte und in Variablen schreiben möchte.

Das Auslesen funktioniert so weit, aber wie bekommen ich die Daten in eine Variable?

So schaut die XML Ausgabe aus

Code:

<?xml version="1.0" encoding="UTF-8"?>

-<presets>


-<preset id="1">


-<ContentItem isPresetable="true" sourceAccount="" location="4188" source="INTERNET_RADIO">

<itemName>Antenne 1 Hit Radio</itemName>

</ContentItem>

</preset>


-<preset id="2">


-<ContentItem isPresetable="true" sourceAccount="" location="7297" source="INTERNET_RADIO">

<itemName>DIE NEUE 107.7 FM</itemName>

</ContentItem>

</preset>


-<preset id="3" updatedOn="1449651242" createdOn="1449651242">


-<ContentItem isPresetable="true" sourceAccount="" location="http://api.deezer.com/user/me/flow" source="DEEZER" type="tracklistRadio">

<itemName>##TRANS_flow##</itemName>

</ContentItem>

</preset>

</presets>

Hier mein PHP Script

Code:

<?

$BoseSoundTouchSystemIP = "192.168.0.52";

$URLString = "presets";

$SystemAvailable = false;
$SystemAvailable = (boolean)Sys_Ping($BoseSoundTouchSystemIP, 1000);
if ($SystemAvailable == false)
        {
        echo "Bose SoundTouch System ist nicht verfügbar!";
        }
else
        {
        // Curl Get
        $Curl = curl_init();

        curl_setopt_array($Curl, array(
                                              CURLOPT_URL => 'http://'.$BoseSoundTouchSystemIP.':8090/'.$URLString,
                                              CURLOPT_HEADER => 0,
                                              CURLOPT_RETURNTRANSFER => 1
                                              ));

        $Response = curl_exec($Curl);

        $ResponseUTF8 = utf8_decode($Response);
        echo "Response: ".$ResponseUTF8."\n\n";
echo 'http://'.$BoseSoundTouchSystemIP.':8090/'.$URLString."\n\n";
        $XMLData = new SimpleXMLElement ($Response);


///////////////////////////
}
$xml = $XMLData;
$vals = array();
RecurseXML($xml,$vals);
foreach($vals as $key=>$value)
  print("{$key} = {$value}<BR>\n");
function RecurseXML($xml,&$vals,$parent="") {

  $childs=0;
  $child_count=-1;
  $arr=array();
        foreach ($xml->children() as $key=>$value) {
                if (in_array($key,$arr)) {
                        $child_count++;
                } else {
                        $child_count=0;
                }
                $arr[]=$key;
                $k=($parent == "") ? "$key.$child_count" : "$parent.$key.$child_count";
                $childs=RecurseXML($value,$vals,$k);
                if ($childs==0) {
                        $vals[$k]= (string)$value;
                }
        }
}
?>

und als Ergebnis bekomme ich die Daten welche ich als Variable speichern möchte

Response: Antenne 1 Hit RadioDIE NEUE 107.7 FM##TRANS_flow##

http://192.168.0.52:8090/presets

preset.0.ContentItem.0.itemName.0 = Antenne 1 Hit Radio
preset.0.ContentItem.0 =
preset.0 =
preset.1.ContentItem.0.itemName.0 = DIE NEUE 107.7 FM
preset.1.ContentItem.0 =
preset.1 =
preset.2.ContentItem.0.itemName.0 = ##TRANS_flow##
preset.2.ContentItem.0 =
preset.2 =

Domi 19.02.2016 14:06:47

AW: XML Datei auslesen und in Variable speichern
 
Hi Wildsau,

konntest Du Dein Problem mittlerweile lösen?

Domi


Alle Zeitangaben in WEZ +2. Es ist jetzt 15:19:22 Uhr.

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