-3

in the past I loaded the xmlfile with <station></station> Tags and did a foreach loop like this

foreach ( $xml->station as $item )
{
...
}>

Now I got a new structure like this and it doesn't work anymore. Can you help me, please?

<stations>
<station id="1" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION"/>

<station id="2" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION"/>
</stations>

Thanks a lot in advance! :-)

AndreS
  • 9
  • 3
    1) Any errors? 2) Will you post any code? 3) XML yoy posted is not valid xml. It has no ` – Forien Jan 14 '15 at 14:20
  • In the past you haved same file but without `` tag ? – anaconda Jan 14 '15 at 14:24
  • I cannot post the complete XML-File because it has a size of 1MB, so its very big. This was only a small snapshot of the XML file. The original file beginns with I download the file from a customer from us, and in the past the XML File was with and now it is with /> – AndreS Jan 14 '15 at 14:29

1 Answers1

0
  • added simplexml_load_string()
  • if you need to load a file $xml = simplexml_load_file('test.xml');
  • added xml doctype line missing.

Example below:

$xml_string = '<?xml version="1.0" encoding="UTF-8"?>
<stations>
  <station id="1" name="Raumschiff1" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION" />
  <station id="2" name="Raumschiff2" strasse="Musterstrasse 1" plz="11011" ort="Berlin" lat="50.77548" lng="6.08147" operator="NEO" telefon="0800 MATRIX" zugang="ZION" />
</stations>';

$xml = simplexml_load_string($xml_string);    
//print_r($xml);

// iterate over the SimpleXMLElement Object    
foreach($xml->station as $station) {

    // you need to take the attributes into account
    foreach ($station->attributes() as $attributeName => $attributeValue) {
        printf("\n%s => %s", $attributeName, $attributeValue);
    }

    echo "<hr>";
}

And your next question is: "How to iterate that object or array?"

Already answered:

Community
  • 1
  • 1
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Now I got this output [stations] => SimpleXMLElement Object ( [station] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 [name] => Raumschiff 1 [strasse] => Musterstrasse 1 [plz] => 11011 [ort] => Berlin [lat] => 11.11111 [lng] => 0.08147 [operator] => NEO [telefon] => 0800 11111 Now I tried to access the attributes with foreach ( $array->station->id as $item ) but it doesnt work. – AndreS Jan 14 '15 at 15:19
  • That's simply an iteration of the values of an object or array. I've updated my answer accordingly. – Jens A. Koch Jan 14 '15 at 20:00