A small SimpleXML gotcha (with namespaces)

I've so far worked only very little with XML with namespaces, especially in terms of parsing it. Therefor, I was quite stumped today on the parsing of a particular snippet of XML which contained an element that had an xlink:href attribute. I just couldn't get that attribute's value out. The solution, as often seems to be the case, is quite simple.

It was my colleague Dynom who saved the day for me. He pointed me towards the second optional parameter of the attributes() method of the SimpleXMLElement object. Given the following snippet (within a larger snippet):

a text

I was using the first parameter of attributes (the namespace) to let SimpleXML know from which namespace I wanted to get the attributes:

$attributes = $xml->a->attributes('xlink' );
echo $attributes['href'];

This didn't work. $attributes['href'] didn't exist, and var_dump-ing the $attributes array returned an empty array. However, clearly I should learn to read better, because the manual does mention the second parameter $is_prefix, which is a boolean that indicates if the attributes are actually prefixed by the namespace. As, in the above case, this is true, I have to set it to true (it defaults to false). So with this it worked:

$attributes = $xml->a->attributes('xlink', true);
echo $attributes['href'];

These are the kinds of details that you can get stumped on for ages, so I hope that people reading this will keep it in mind and remember it when they encounter a similar situation :)


Add comment

Comments

gravatar : what all those smilies?
June 4, 2008
gravatar left: good point, I removed the smiley from the code example. I hadn't noticed it for some reason.

I also noticed from your comment another bug ... the name isn't a requirement it seems :)
June 4, 2008
gravatar arne: Hey, thanks for the tip :)

It's saves me a lot of trying!
August 12, 2008
gravatar leon: Saved my week :)
June 12, 2009
gravatar James: Getting attributes to show with namespaces had me stumped for an hour. This article put me on the right track. Thanks so much!!!
June 26, 2009

Php5_zce_logo

not tested in IE


Upcoming events

I will be speaking 04-09-2009: Symfony Day 2009

Tags

1337 2008 4developers accessibility AdaLovelaceDay09 agavi agile amsterdam apache apple article articles atk atkMetaNode audioscrobbler backwards compatibility bbc beatstad best practices bittorrent book books bughuntday caching cake cal evans cat cerf certificate cfp clear cms community conference conferences continuous integration crisis css custom DbFinderPlugin decorator decorators deployment devdays development directoryindex documentation download dpc dpc09 DPC2008 dreamhost eclipse ed efficiency enterprise event events expertise flickr frameworks freeze frontend fun games germany getting real google googletalk graceful degradation hack hackers howto html http ibuildings icann ide imovie indy internet IPC ipc ipc08 javascript jobeet john peel joomla left on the web lime linux live london loudblog m2ts mac malware mambo marjolein meme meta methodology microsoft movie music mysql namespace namespaces nllgg odmarco open source ORM osx paradiso pear personal pfcongrez photo php phpabstract phpBB phpbb phpbelgium phpbenelux phpgg phpitalia phpnw phpnw08 phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations public qa recruiting refactoring review rewrite ruby on rails schedule script security seven things simplexml slides software sogeti solar standard standards static steer subversion symfony symfonycamp symfonyUnderControlPlugin talk technology techportal tek09 terratec terrorism testfest testing textpattern tips tld tomas usability usergroup vhost video vinyl virus warp weblogging wiki women world world of warcraft writing xml xpath yara year youtube ZCE zemanta zend zend framework zend server zend studio Zend_Form
© 2004 - 2009 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0