Zabi
Zabi

Reputation: 23

PHP SimpleXML - Get Attribute Values - Loop

i don't know why am I not getting all results what i want from the xml file.

Here's my code:

<?php
$xml=simplexml_load_file("http://2strok.com/gen/maler.xml") or 
     die("Error: Cannot create object");
    foreach($xml->children() as $books) { 
    echo $books->XResult->Contacts->XContact->Name . "<br>";
    echo $books->XResult->Contacts->XContact->Value . "<br>"; 
    echo $books->XResult->Contacts->XContact->VisitationAddress . "
    <br>"; 
            } 
?> 

I'm using php foreach but i'm getting only the first line :(

Upvotes: 1

Views: 392

Answers (1)

Syscall
Syscall

Reputation: 19780

You could try something like this :

$xml=simplexml_load_file("http://2strok.com/gen/maler.xml") or
     die("Error: Cannot create object");
foreach($xml->ResultList->XResult as $res) {
    if ($res->Contacts->XContact) {
        echo $res->Contacts->XContact->Name . "<br>";
        echo $res->Contacts->XContact->Value . "<br>";
        echo $res->Contacts->XContact->VisitationAddress . "<br>";
    }
}

Or this, if you want all contacts :

$xml=simplexml_load_file("http://2strok.com/gen/maler.xml") or
     die("Error: Cannot create object");
foreach($xml->ResultList->XResult as $res) {
    foreach ($res->Contacts->XContact as $elm) {
        echo $elm->Name . "<br>";
        echo $elm->Value . "<br>";
        echo $elm->VisitationAddress . "<br>";
    }
}

Upvotes: 2

Related Questions