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
May 30, 2008 - tags: simplexml, xml, namespaces, php
Comments

: what all those smilies?
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
arne: Hey, thanks for the tip
It's saves me a lot of trying!

leon: Saved my week
James: Getting attributes to show with namespaces had me stumped for an hour. This article put me on the right track. Thanks so much!!!

Tails: Saved my day!
Kawika: Hey, thanks for this post. XML namespace parsing with SimpleXML has had me stumped for hours today, and this post helped a lot.
Round Sticker Printing: i agree with you.these parameter of attributes are good and help us.you discus a good topic int his post..thanks for sharing with us..
Tyler Morning Telegraph: you discus some good points in this post.
i like your post you do good effort to make it more valuable.
thanks for sharing.
secrets to weight loss: this work is so good and useful with a small Simple XML.i like to read informative blogs and this blog is also so good and helpful.thanks for taking time to discus this topic..
orlando screen room: I never looked at it like this before. I did not know that you could do this. I am glad to have this option.
fprsmag: Hey, thanks for this post. XML namespace parsing with SimpleXML has had me stumped for hours today, and this post helped a lot.
eros upcoming project: Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.
eros projects in gurgaon: I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
does nono work: I've been analyzing your own articles frequently. I have to declare that you are doing a amazing job. Remember to keep up the truly great writing.
Ansoo Lake: Interesting topic what you have shared with us. Your writing skill is really very appreciative. I love when you share your views through the best articles.Keep sharing and posting articles like these.This article has helped me a lot.Keep posting this stuff.
5htp dosage withdrawal: 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,
clarinet supplies buy: 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 indic
dimethylpentylamine dimethylamylamine: f 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 situati
shizandrol a drug test: f'] 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
yohimbine hydrochloride usp: first parameter of attributes (the namespace) to let SimpleXML know from which namespace I wanted to get the attributes:

furniture stores phoenix:
Hi buddy, your blog's design is simple and clean and i like it. Your blog posts are superb.
Please keep them coming. Greets!!!
coach outlet online: If you buy Coach items at the
coach outlet online
store, the goods will be sent out within 24 hours after confirming your payment and arrive to your door within 7 work days.No one can deny the shopping at the
coach factory outlet
is satisfactory. For the low prices and good quality.
coach outlet: coach outlet
handbags You can not just save your cash but also your time inside a convenient way to obtain the exact same bags product and do not be concerned the shippment of one's products.The
coach outlet store online
are utilized for formal event in company area that will display your excellent taste.Not only does a briefcase make is easier to carry things.
modern furniture phoenix az: I am sure my visitors will locate that extremely useful.
discount uggs boots: Your first-class knowledge about this good post can become a proper basis for such people. nice one!
michael kors outlet: Thank you for this great resource, these articles are very helpful (so many left to read). Awesome job and thanks again for doing something for other people on your day.
Pillows: I think this is a nice place. Thanks for taking the time to post such an informative article. It has given me a lot of inspiration.
game of thrones: thx code works like a charm