0

I have a SimpleXMLElement Object that I want to import with PHP. But it only loop through the first row in the XML file. I also want to show the specific columm with $item->columm1 instead of show the whole row whith all it's values .

$url = 'file.xml';
$sxml = simplexml_load_file($url);

foreach($sxml->Departure->attributes() as $item)
{
    echo $item;
}

EDIT: Here is the output, notice that I have edit the orginal values with text1, text2.

SimpleXMLElement Object ( 
[Departure] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => text1 [type] => text2 [stop] => text3 [time] => text4 [date] => text4 [direction] => text5 )

EDIT2: Output->

object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(6) { ["name"]=> string(6)     "text1" ["type"]=> string(3) "text2" ["stop"]=> string(12) "text3" ["time"]=> string(5) "text4" ["date"]=> string(8) "text5" ["direction"]=> string(10) "text6" } ["JourneyDetailRef"]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["ref"]=> string(125) "text7" } } } 

EDIT3: Output->

  object(SimpleXMLElement)#7 (1) { [0]=> string(6) "text1" } object(SimpleXMLElement)#8 (1) { [0]=> string(3) "text2" } object(SimpleXMLElement)#7 (1) { [0]=> string(12) "text3" } object(SimpleXMLElement)#8 (1) { [0]=> string(5) "text4" } object(SimpleXMLElement)#7 (1) { [0]=> string(8) "text5" } object(SimpleXMLElement)#8 (1) { [0]=> string(10) "text6" } 

1 Answer 1

1

$item has become the object. When you want to loop over the item, make sure you tell what part of the item you want to echo out to the screen.

For example:

$url = 'file.xml';
$sxml = simplexml_load_file($url);

//foreach loop
foreach($sxml->Departure as $item){
    //vardump
    var_dump($item);

    //construct next piece of code
    foreach($item->attributes() as $key => $piece){
         echo $key.' = '.$piece;
    }
}
5
  • Do you have an example of the XML data?
    – Telshin
    Commented Jun 22, 2012 at 15:17
  • 1
    @user1273409 Try running print_r($sxml); or var_export($sxml); and looking at the structure of the object. From there you should be able to discern callable functions. Another option could be that once inside the foreach do this: die(var_export($item, true));. That will halt execution and print the structure of $item. From there you should be able to see what $item is and figure out if you're at the right place in the XML hierarchy and what to call.
    – Eric H
    Commented Jun 22, 2012 at 15:26
  • @Tim I have added the output at the top. Commented Jun 22, 2012 at 15:40
  • Have you tried debugging each step of the foreach loop, so you can see the output? I put an example in the answer.
    – Telshin
    Commented Jun 22, 2012 at 16:23
  • That looks better. But Is it possible to show only one Attribute with a variable. So I could change the order of the them. $piece->name and $piece->type like mysql with $row Commented Jun 22, 2012 at 18:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.