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 wrote a book titled Professional Web APIs with PHP back in 2006, and am currently working in Biomedical Informatics for a major public health company.

I’m working on a project to help developers called WonderProxy which has proxies all over the world. Working on GeoIP development? Now you can finally test properly! We've also released Global Ping Statistics for expected ping times between cities, as well as a Load Testing Tool to measure your site's ability to handle load. Our most recent site checking tool is Where's it Up? which checks your sites availability globally, returning HTTP, DNS, and Traceroute details

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

I co-founded:

WonderNetwork Logo

Search