-1

I have an xml feed which I need to access an element from. However, none of the methods I have tried have worked.

Below is the xml feed in full:

<GeocodeResponse>
<status>OK</status>
<result>
    <type>locality</type>
    <type>political</type>
    <formatted_address>Liverpool, Merseyside, UK</formatted_address>
    <address_component>
        <long_name>Liverpool</long_name>
        <short_name>Liverpool</short_name>
        <type>locality</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Merseyside</long_name>
        <short_name>Mersyd</short_name>
        <type>administrative_area_level_2</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>England</long_name>
        <short_name>England</short_name>
        <type>administrative_area_level_1</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>United Kingdom</long_name>
        <short_name>GB</short_name>
        <type>country</type>
        <type>political</type>
    </address_component>
    <geometry>
        <location>
            <lat>53.4115400</lat>
            <lng>-2.9901160</lng>
        </location>
        <location_type>APPROXIMATE</location_type>
        <viewport>
            <southwest>
                <lat>53.3049930</lat>
                <lng>-3.2462348</lng>
            </southwest>
            <northeast>
                <lat>53.5178208</lat>
                <lng>-2.7339972</lng>
            </northeast>
        </viewport>
        <bounds>
            <southwest>
                <lat>53.3115426</lat>
                <lng>-3.0191794</lng>
            </southwest>
            <northeast>
                <lat>53.5039071</lat>
                <lng>-2.8115043</lng>
            </northeast>
        </bounds>
    </geometry>
</result>
</GeocodeResponse>

I am trying to select geometry->location->lat->location and then within this lat and lng.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Daniel Benzie
  • 479
  • 1
  • 5
  • 27
  • [XML Parser to the rescue!](http://php.net/manual/en/book.xml.php) –  Feb 09 '12 at 22:32
  • 1
    Is there a PHP question here? Have any code? How are you reading the file? You say nothing has worked - but what did you try? – Wesley Murch Feb 09 '12 at 22:32
  • 3
    possible duplicate of [How to Parse XML File in PHP](http://stackoverflow.com/questions/1706042/how-to-parse-xml-file-in-php) – Wesley Murch Feb 09 '12 at 22:35
  • possible duplicate of [A simple program to CRUD node and node values of xml file](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Feb 09 '12 at 22:44

2 Answers2

2

Have you tried simplexml_load_string or simplexml_load_file? Simply load your data with it and then access the node you want, like this:

$xml = simplexml_load_file('your.xml');
echo $xml->GeocodeResponse->result->geometry->location->lat;
echo $xml->GeocodeResponse->result->geometry->location->lon;
Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • sorry i should have been more specific i am getting an error but that appears to be by the by now because it was my simple xml loader that wasnt working. I opened openssl in php.ini and all is well again. ]]sorry for wasting everybodies time I have + all comments. thanks – Daniel Benzie Feb 09 '12 at 22:45
  • @DanielBenzie Please update your question with this information, without all the proper details we won't be able to come up with a good answer. – Oldskool Feb 09 '12 at 22:48
0

If you have a string, you can do:

$str = "<?xml version='1.0'?> 
            <document> 
                <cmd>login</cmd> 
                <login>Richard</login> 
            </document>";
$xml = simplexml_load_string($str);

print_r($xml);

print_r($xml->document->cmd);

// Remember to type cast it a string if you want the actual value.
// Example:
$myval = (string) $xml->document->cmd;
Josh
  • 8,082
  • 5
  • 43
  • 41
john smith
  • 733
  • 3
  • 8
  • 18