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_name>
2) <short_name>M20</short_name>
3) <short_name>T66</short_name>
4) <short_name>T64</short_name>


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

Open in new window

Jeremy LeysTechnical LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeremy LeysTechnical LeadAuthor Commented:
Found the issue myself, I wasn't iterating through route, needed a nested foreach

function build_transit_details($xml_obj){
	
	$transport = "";
	
	if(isset($xml_obj->route)){
	
		foreach($xml_obj->route as $k => $v){
			
			if(isset($v->leg->step)){
				
				foreach($v->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;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.