Jeremy Leys
asked on
Trouble treversing XML with simple xml
Hi,
I am having trouble traversing an XML file to get the data I want using simple xml with PHP
XML file:
https://maps.googleapis.com/maps/api/directions/xml?origin=61+-+65+Wentworth+Ave%2C+Sydney&key=AIzaSyCLFuUAdybqWbIOEJoyYLgf9Y3-MjtKu3s&destination=10+Norbrik+Dr&mode=transit&alternatives=true
What I want to do is extract each (line short_name or name) and each (line vehicle name or vehicle type
My code only gives me the first 607x Bus | but there are several
1) <short_name>607x</short_na me>
2) <short_name>M20</short_nam e>
3) <short_name>T66</short_nam e>
4) <short_name>T64</short_nam e>
I am having trouble traversing an XML file to get the data I want using simple xml with PHP
XML file:
https://maps.googleapis.com/maps/api/directions/xml?origin=61+-+65+Wentworth+Ave%2C+Sydney&key=AIzaSyCLFuUAdybqWbIOEJoyYLgf9Y3-MjtKu3s&destination=10+Norbrik+Dr&mode=transit&alternatives=true
What I want to do is extract each (line short_name or name) and each (line vehicle name or vehicle type
My code only gives me the first 607x Bus | but there are several
1) <short_name>607x</short_na
2) <short_name>M20</short_nam
3) <short_name>T66</short_nam
4) <short_name>T64</short_nam
function build_transit_details($xml_obj){
$transport = "";
foreach($xml_obj->route->leg->step as $key => $val){
if(isset($val->transit_details->line)){
if(isset($val->transit_details->line->short_name)){
$transport .= $val->transit_details->line->short_name." ";
}elseif(isset($val->transit_details->line->name)){
$transport .= $val->transit_details->line->name." ";
}
if(isset($val->transit_details->line->vehicle->name)){
$transport .= $val->transit_details->line->vehicle->name." | ";
}elseif(isset($val->transit_details->line->vehicle->type)){
$transport .= $val->transit_details->line->vehicle->type." | ";
}
}
}
echo $transport;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.