我是 xpath 的新手,无法解析它。它不产生任何输出。
这包含从 Google Map API V3 返回的 XML。我想让它解析并从 PostalCode/PostalCodeNumber 返回邮政编码。我设置了这个测试,因为我需要能够确保我可以访问 PostalCode/PostalCodeNumber,即使 Google 的 API 会阻碍其他标签,有时会这样做。因此,让 xpath 正常工作对我来说很重要,因为我希望能够直接访问 PostalCode/PostalCodeNumber 等内容,而无需依赖静态树结构路径。请善意和帮助您的回复,谢谢!
<?php
$xml = <<<XML
<?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>
XML;
$xml = new SimpleXMLElement($xml);
$result = $xml->xpath('PostalCode/PostalCodeNumber');
while(list( , $node) = each($result)) {
echo 'PostalCode/PostalCodeNumber: ',$node,"\n";
}
?>