Google Map API V3에서 반환 된 XML을 구문 분석하고 있습니다. 다음은 전형적인 XML가 반환됩니다PHP에서이 xpath 객체를 구문 분석하는 좀 더 우아한 방법이 있습니까?
North: 40.7458050
South: 40.7431070
East: -73.9736017
West: -73.9762997
array(1) {
[0]=>
object(SimpleXMLElement)#10 (1) {
["@attributes"]=>
array(4) {
["north"]=>
string(10) "40.7458050"
["south"]=>
string(10) "40.7431070"
["east"]=>
string(11) "-73.9736017"
["west"]=>
string(11) "-73.9762997"
}
}
}
:
여기$xml = new SimpleXMLElement($url, null, true);
$xml->registerXPathNamespace('http', 'http://earth.google.com/kml/2.0');
$LatLonBox_result = $xml->xpath('//http:LatLonBox');
echo "North: " . $LatLonBox_result[0]["north"] . "\n";
echo "South: " . $LatLonBox_result[0]["south"] . "\n";
echo "East: " . $LatLonBox_result[0]["east"] . "\n";
echo "West: " . $LatLonBox_result[0]["west"] . "\n";
var_dump($LatLonBox_result);
이 편집 된 출력 : 여기에
<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>40.74445606,-73.97495072</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>317 E 34th St, New York, NY 10016, USA</address>
<AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><Locality><LocalityName>New York</LocalityName><Thoroughfare><ThoroughfareName>317 E 34th St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>10016</PostalCodeNumber></PostalCode></Locality></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.7458050" south="40.7431070" east="-73.9736017" west="-73.9762997" />
</ExtendedData>
<Point><coordinates>-73.9749507,40.7444560,0</coordinates></Point>
</Placemark>
</Response></kml>
내가 그것을 구문 분석하는 데 사용하고 PHP 코드의 조각이다 $ LatLonBox_result [0] [ "north"]를 사용하면 추한 것처럼 보입니다. 이것이 xpath로 작업 할 때의 상황입니까? 나는 $ LatLonBox_result [ "north"]와 같은 반환 값을 기대하고 있었고 배열의 첫 번째 차원을 가지고 있지 않았습니다. 아니면이 접근 방식이 잘못 되었습니까? 더 좋은 방법이 있다면 제게 친절히 알려주십시오. 감사!
Nope thats ... xpath는 하나만있는 경우에도 항상 배열을 반환합니다. – prodigitalson