Reading XML with PHP

I’m currently using the simplexml_load_file() class however I dont know how to read deep into the XML. Currently I am trying to read from google and it wont go past the result section.

The xml looks as such


<GeocodeResponse>
<status>OK</status>

<result>
<type>street_address</type>

<formatted_address>
1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA
</formatted_address>

<address_component>
<long_name>1600</long_name>
<short_name>1600</short_name>
<type>street_number</type>
</address_component>

<address_component>
<long_name>Amphitheatre Pkwy</long_name>
<short_name>Amphitheatre Pkwy</short_name>
<type>route</type>
</address_component>

<address_component>
<long_name>Mountain View</long_name>
<short_name>Mountain View</short_name>
<type>locality</type>
<type>political</type>
</address_component>

<address_component>
<long_name>San Jose</long_name>
<short_name>San Jose</short_name>
<type>administrative_area_level_3</type>
<type>political</type>
</address_component>

<address_component>
<long_name>Santa Clara</long_name>
<short_name>Santa Clara</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>

<address_component>
<long_name>California</long_name>
<short_name>CA</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>

<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>

<address_component>
<long_name>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>

<geometry>

<location>
<lat>37.4220323</lat>
<lng>-122.0845109</lng>
</location>
<location_type>ROOFTOP</location_type>

<viewport>

<southwest>
<lat>37.4188847</lat>
<lng>-122.0876585</lng>
</southwest>

<northeast>
<lat>37.4251799</lat>
<lng>-122.0813633</lng>
</northeast>
</viewport>
</geometry>
</result>
</GeocodeResponse>

The code I am using in php is as follows and I cant get it to work.



$xml = simplexml_load_file("http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true");	

foreach($xml->children() as $child)
{
   echo $child->getName()." - ".$child."<br>";
}

Hope something like this will help you go in right direction and find more from xml:


$xml = simplexml_load_file("http://maps.google.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true"); 

if($xml->children()->status == 'OK'){
    echo 'Type : ' . $xml->children()->result->type . '<br />';
    echo 'Formatted Address : ' . $xml->children()->result->formatted_address . '<br />';
    $addresses = $xml->children()->result->address_component;
    foreach($addresses as $address){
        echo $address->long_name . ' X ' . $address->short_name . '<br />';
    }
}

can someone please delete the user is_set he’s sending me spam mail to go to other sites to get the answers to my questions.