SimpleXML and Reading CDATA Tags

Wednesday, January 30. 2008

While using PHP5, almost everyone knows that SimpleXML is the easiest class to utilize when absorbing data from an XML source (be it XML, Atom, RSS, etc...). But, there are instances where things aren't as smooth as one would hope. I was reading in some RSS feeds from WeatherBug the other day, and was all fine and dandy, until I came to an annoying little snippet of code within the feed.


When I got to that data, SimpleXML couldn't retrieve it. When I did a dump of the entire feed, SimpleXML expressed the description tags as being empty. Ok...so how the heck can we get at it?

It's much simpler than one would expect. You forcibly convert the value of that field to a string. That's it.


The annoyingly difficult thing about this, is that I knew I wouldn't be able to figure it out on my own, and the PHP docs didn't help with that one. I went to Google, and that took a few tricky searches to find what I needed too!

Source: Using SimpleXML to Parse RSS Feeds

Tip: That link also shows how to traverse different namespaces, just in case your RSS or XML sources use namespaces, which the last time that happened I used a DOMDocument object, which was a little more work than necessary. I wish I knew this back then!

Trackbacks


Trackback specific URI for this entry
    No Trackbacks

Comments


    No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

BBCode format allowed