We help IT Professionals succeed at work.

return part of xml to JSON after a xpath search

Neil Thompson
Hi all

I want to be able to pass a "wardTitle" variable to the XML from this page and be able to save just the resulting details as json.

The following returns a XML file of all our councillors and their details. I want to be able to filter on the passed "wardTitle" and only then return the details associated with the councillors who come under that wardtitle


For example If I pass "Cheriton Ward" only the node <wardtitle>Cheriton Ward</wardtitle> would be returned with all it's children so thats the <councillors node> and 3 <councillor> nodes with their associated content

I have tried using xpath but It then seems really cumbersome to iterate through every single node rather than just be able to grab that and convert it to json

one of the 3 councillor nodes for Cheriton ward

I hope that makes sense, if not please shout :(
Watch Question

Systems architect
Top Expert 2006

Check this and remove  JSON_PRETTY_PRINT  if you do not need it:

< ?php 
$url = "https://www.folkestone-hythe.gov.uk/moderngov/mgWebService.asmx/GetCouncillorsByWard"; 
$xml = simplexml_load_file($url); 
$result = $xml->xpath("//wardtitle[. = 'Cheriton Ward']/parent::*"); 
echo json_encode($result, JSON_PRETTY_PRINT);