Skip to main content
lovewebdev
Inspiring
September 15, 2009
Question

Retrieving XML with Coldfusion

  • September 15, 2009
  • 1 reply
  • 939 views

I generally retrieve xml tags like so

<videos>

<video>

<thumbnail_small>

</thumbnail_small>

</video>

</videos>

<cfset temp = QuerySetCell(myquery, "thumbnail_small", #mydoc.videos.video.thumbnail_small.XmlText#, #i#)>

Im trying to do something similar with youtube's api found here, but the xml is slightly different:

http://code.google.com/apis/youtube/2.0/developers_guide_protocol_understanding_video_feeds.html#Understanding_Feeds_and_Entries

How can I edit my code to retrieve the value for this tag?

 <media:thumbnail>
This topic has been closed for replies.

1 reply

ilssac
Inspiring
September 15, 2009

First option would be to use array notation.

mydoc.videos.video.thumbnail_small.XmlText 

Is the same as

mydoc['videos']['video']['thumbnail_small']['XmlText']

This allows for keys that would be illegal in dot notaion such as ['media:thumbnail']

Secondly you may want to learn more about some of the functions that let you work with xml data, such as xmlSearch() and such.

Thirdly you may want to see about using xml stylesheets to dig into the xml data to get it the way you want it.

lovewebdev
Inspiring
September 15, 2009

I have

<cfset temp = QuerySetCell(myquery, "thumbnail", mydoc['entry']['media:group']['media:thumbnail']['XmlText'])> 

I'm getting nothing returned and no errors

the xml tree is like that

<entry><media:group><media:thumbnail>

...closing tags

my full code is

<cfhttp url="http://gdata.youtube.com/feeds/api/videos/zGAm929CEoE" method="GET" resolveurl="No" ></cfhttp>
<cfset mydoc = XmlParse(CFHTTP.FileContent)>


<cfset myquery = QueryNew("thumbnail")>
<cfset temp = QueryAddRow(myquery, #size#)>

<cfset temp = QuerySetCell(myquery, "thumbnail", mydoc['entry']['media:group']['media:thumbnail']['XmlText'])> 
<cfoutput>
#myquery.thumbnail#
</cfoutput>

ilssac
Inspiring
September 15, 2009

Also be aware of how ColdFusion handles namespaces in XML data.  It can be a bit tricky to work with them in CFML.

http://www.google.com/search?q=coldfusion+xml+namespace&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html

http://www.talkingtree.com/blog/index.cfm/2005/11/18/XmlSearchNoNameNamespace