Possible Duplicate:
Parse XML with Namespace using SimpleXML
I'm new to xpath, and unable to get this to parse. It produces no output.
This contains XML returned from Google Map API V3. I want to have it parse and return the Zip Code from the PostalCode/PostalCodeNumber. I set-up this test because I need to be able to make sure I can get to the PostalCode/PostalCodeNumber even if Google's API puts other tags in the way, which is does sometimes. So it's important for me to get the xpath working because I want to be able to get direct access to things like PostalCode/PostalCodeNumber without relying on static tree structure path. Please be kind and helpful on your reply, thanks!
<?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"; } ?>
(urn:oasis:names:tc:ciq:xsdschema:xAL:2.0)PostalCodeand yourPostalCode, for example, don't match. You need to specify a namespace, and the highest-voted answer to the other question covers how to do that.