Skip to content
Advertisement

Using SimpleXML to extract data from XML page returning empty array

I’m trying to extract the values of all elements named wardtitle from this xml page: https://democracy.ashfield-dc.gov.uk/mgWebService.asmx/GetCouncillorsByWard

Here’s the code I’m currently trying:

$xml = simplexml_load_file("https://democracy.ashfield-dc.gov.uk/mgWebService.asmx/GetCouncillorsByWard");
var_dump($xml->children());

$ward = (string) $xml->wardtitle;
echo $ward;

print_r($xml->xpath("//email"));

The children dump seems to work fine, the $ward variable returns nothing, then the xpath attempt returns the correct number of results, but all empty… Any help very much appreciated.

Advertisement

Answer

According to xml structure: wardtitle is a node of each ward, which in turn – node of wards, so, to echo wardtitle of first ward:

echo $xml->wards->ward[0]->wardtitle;

As for emails – everything works fine:

foreach ($xml->xpath("//email") as $email) {
    echo $email;
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement