Parsing XML Elements with Colons

You may have noticed my Woot Tag over on the right.  Once per day (unless refreshed by a user) the tag will read Woot.com’s blog RSS feed and display the item for sale that day.  I ran into trouble parsing through the elements in the XML feed using ColdFusion.



I had no trouble as long as the XML looked like this:



<rss version="2.0">

    <channel>

        <title>Woot – One Day, One Deal</title>

        <link>http://www.woot.com/Blog/</link>

        <description>Woot.com – One Day, One Deal!</description>

    </channel>

</rss>


But then, further down in the item elements I came across this type of syntax:



<link>http://www.woot.com/Blog/BlogEntry.aspx?BlogEntryId=1088</link>

<pubDate>Tue, 18 Apr 2006 05:00:11 GMT</pubDate>

<media:title>Netgear 802.11g Wireless USB 2.0 Adapter – 2 Pack</media:title>

<media:thumbnail>

    http://www.woot.com/Images/Sale/_Netgear_802.11g_Wireless_USB_2.0_Adapter_-_2_Pack-thumbnail.jpg

</media:thumbnail>


Notice the colons within the child element names.  I didn’t know how to parse through these with ColdFusion.  I got around it by telling my tag to read the 7th element in the XML item structure using the following:



<cfset variables.item_image = application.woot_xmlDoc.xmlRoot.channel.item[variables.counter].XmlChildren[7].xmltext>

This worked, but it seemed like a hack to me.  What if Woot decided to add a new element?  That would break my code.



So finally, today I decided to figure out how to properly handle these "colon elements".  After a lot of searching, I finally found my answer on LiveDocs.  It was kind of hidden down in one of the XML entries:



Use associative array (bracket) notation to specify an element name that contains a period or colon; for example, myotherdoc.XmlRoot["Type1.Case1"].

And then, after a little trial and error, I finally found the proper syntax, which is:



<cfset variables.item_image = application.woot_xmlDoc.xmlRoot.channel.item[variables.counter]["media:thumbnail"].xmltext>

That makes me feel better.



If you want a much clearer explanation of how this is supposed to work, go check out Jeff Houser’s step by step walk-through.




Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>