2012-02-09 6 views
-1

요소에 액세스해야하는 XML 피드가 있습니다. 그러나 내가 시도한 방법 중 어느 것도 효과가 없습니다. 나는이 위도와 LNG 내에서 다음 geometry-> 위치 -> lat-> 위치를 선택하려고 노력하고 있어요PHP의 XML 파일에서 요소를 읽으려면 어떻게해야합니까?

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

:

다음은 전체의 XML 피드입니다.

+0

을 (http://php.net/manual/en/book.xml.php) –

+1

여기에 PHP 관련 질문이 있습니까? 코드가 있습니까? 파일을 어떻게 읽니? 당신은 아무 것도 효과가 없다고 말하지만, 무엇을 시도 했습니까? –

+3

[PHP에서 XML 파일을 구문 분석하는 방법] 가능한 복제본 (http://stackoverflow.com/questions/1706042/how-to-parse-xml-file-in-php) –

답변

2

simplexml_load_string 또는 simplexml_load_file을 사용해 보셨습니까? 단순히 사용하여 데이터를로드하고 다음과 같이 원하는 노드에 액세스 :

$xml = simplexml_load_file('your.xml'); 
echo $xml->GeocodeResponse->result->geometry->location->lat; 
echo $xml->GeocodeResponse->result->geometry->location->lon; 
+0

죄송합니다. 더 구체화되었습니다 오류가 발생했습니다. 작동하지 않는 간단한 xml 로더 였기 때문에 지금까지는 보인다. php.ini에서 openssl을 열었고 모든 것이 잘되었습니다. ]] 모든 시간을 낭비해서 죄송합니다. 덕분에 –

+0

@ 대니얼 벤지 (D Daniel Benzie)이 정보로 질문을 업데이트하십시오. 적절한 세부 정보가 없으면 좋은 대답을 찾을 수 없습니다. – Oldskool

0

당신이 문자열이있는 경우, 당신은 할 수 있습니다 : [! XML 파서 구출]

$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; 
관련 문제