php – Using SimpleXML to extract data from XML page returning empty array-ThrowExceptions

Exception or error:

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.

How to solve:

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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *