Take a little look at this little code block:

$response = <<< XMLBLOCK<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2005-10-28T01:01:04.668Z</Timestamp><Ack>Success</Ack><Version>429</Version><Build>e429_intl_Bundled_1949355_R1</Build></GeteBayOfficialTimeResponse></soapenv:Body></soapenv:Envelope> XMLBLOCK; $xml = simplexml_load_string($response);

Now, imagine that for some crazy reason, you care what time it is in eBay land, how would you access that Timestamp value? Your first thought is probably something along the lines of:

echo "Time: " . $xml->Envelope->Body->GeteBayOfficialTimeResponse->Timestamp . "\n";

Quaint, nice try, but you're not even close. If you're really paying attention you noticed the namespaces and might try one of these:

echo "Time: " . $xml->{'soapenv:Envelope'}->{'soapenv:Body'}->GeteBayOfficialTimeResponse-> Timestamp . "\n"; echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body-> GeteBayOfficialTimeResponse->Timestamp . "\n";

Or noticing that the body is also in the namespace, this:

echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')-> children('http://schemas.xmlsoap.org/soap/envelope/')->GeteBayOfficialTimeResponse-> Timestamp . "\n";

Nope, It's this:

echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')-> children('urn:ebay:apis:eBLBaseComponents')->GeteBayOfficialTimeResponse->Timestamp . "\n";

In retrospect, it seems to make a lot of sense. However, all the documentation I've read on namespaces in SimpleXML tell you that you need to pass it the URL of the relevant namespace (the aliases are relatively irrelevant). It would be great if the print_r() or var_dump() output gave you some deep indication of how the data should be accessed, but alas, it does not:

SimpleXMLElement Object ( [Body] => SimpleXMLElement Object ( [GeteBayOfficialTimeResponse] => SimpleXMLElement Object ( [Timestamp] => 2005-10-28T01:01:04.668Z [Ack] => Success [Version] => 429 [Build] => e429_intl_Bundled_1949355_R1 ) ) )

Comments »

No Trackbacks
$xml->children('soapenv', true)->children()->GeteBayOfficialTimeResponse->Timestamp
#1 Kenneth Kin Lum on 2009-04-23 21:54 (Reply)

Thanks for the tip Kenneth, I'll look at updating this poist in the next few weeks.
#2 Paul Reinheimer (Homepage) on 2009-04-23 22:15 (Reply)


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
 

Hi, I’m Paul Reinheimer, a developer working on the web.

I co-founded WonderProxy which provides access to over 200 proxies around the world to enable testing of geoip sensitive applications. We've since expanded to offer more granular tooling through Where's it Up

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.

Search