0

I am trying to convert a xml into an array so i can use it to a JSON. to be used with a jquery code for a city search: jquery

 <script type="text/javascript">   
        $(document).ready(function(){ var ac_config = { source: "cities.php", select: function(event, ui){ $("#City").val(ui.item.City); $("#Country").val(ui.item.Country); $("#DestinationID").val(ui.item.DestinationId); }, minLength:3 }; 
        $("#City" ).autocomplete(ac_config); }); 

        </script> 

Xml Looks like :

<CIties>
        <Destination>
            <DestinationId>1SVV</DestinationId>
            <Country>Canada</Country>
            <City>100 Mile House</City>
            <State>BC</State>
        </Destination>
        <Destination>
            <DestinationId>1X9K</DestinationId>
            <Country>Netherlands</Country>
            <City>1c3d</City>
            <State></State>
        </Destination>
<Cities>

And the array like the below model

$Destinations= array( 
array('City'=>'Rome', Country=>'Italy', DestinationId=>'RHMK'), 
array('City'=>'Los Angeles', Country=>'CA', DestinationId=>'90001'), 
array('City'=>'Chicago', Country=>'IL', DestinationId=>'60601'), );

I have tried:

<?php   // Data could be pulled from a DB or other source 
$xmlstring = file_get_contents("city.xml");
$xml = simplexml_load_string($xmlstring); 
$json = json_encode($xml);
$array = json_decode($json,TRUE);
 // Cleaning up the term
 $term = trim(strip_tags($_GET['term']));   
 // Rudimentary search
 $matches = array(); foreach($cities as $City){ if(stripos($City['City'], $term) !== false){ 
 // Add the necessary "value" and "label" fields and append to result set 
 $City['value'] = $City['City']; $City['label'] = "{$City['City']}"; $matches[] = $City; } }   
 // Truncate, encode and return the results 
 $matches = array_slice($matches, 0, 5); print json_encode($matches);
?>

But is not working

Razvan Baba
  • 155
  • 9
  • Take a look here: http://stackoverflow.com/a/20431742/1557778 – Robi Jan 14 '15 at 17:08
  • @Robi and from here $xml = simplexml_load_string($xmlstring); $json = json_encode($xml); $array = json_decode($json,TRUE); how can i define the location of the xml file – Razvan Baba Jan 14 '15 at 17:15
  • $xmlstring = file_get_contents(PATH TO YOUR XML FILE/URL); hope this helps – Robi Jan 14 '15 at 17:23

0 Answers0