Help - Search - Members - Calendar
Full Version: Cfml ---> Xml
W3Schools Forum > Server Scripting > ColdFusion
Jayesh
anybody can guide me to get data from Xml using Cfml ..?
i want to get data from xml and convert it into cfquery ....
Skemcin
here is some code that will read any well formated RSS 2.0 Feed(which is XML). Play with that and let me know if you need more help:
CODE
<br />
<cftry>
    <!--- get feed from site --->
    <cfparam name="urltopull" default="http://www.npr.org/rss/rss.php?id=1001">
    <cfhttp url="#URLToPull#" method="GET" timeout="15" />
    
    <!--- parse xml information --->
    <cfset objRSS = xmlParse(cfhttp.filecontent)>
    
    <!---
    <cfdump var="#objRSS#" label="RSS Feed">
    <cfabort>
     --->
    
    <!--- find length of arrary to look through --->
    <cfset ArrayLength = arraylen(objRSS.rss.channel.item)>
    <cfset ItemsDisplayed = 0>
    
    <!--- loop through array to output feed --->
    <cfloop index="x" from="1" to="#ArrayLength#">
    <cfoutput>
    <cfset ItemsDisplayed = ItemsDisplayed + 1>
    <strong>#objRSS.rss.channel.item[x].title.xmltext#</strong><br />
    #objRSS.rss.channel.item[x].description.xmltext#<br />
    <em class="smallfont">(#objRSS.rss.channel.item[x].pubdate.xmltext#)</em><br />
    <a href="#replace(objRSS.rss.channel.item[x].link.xmltext,'&','&amp;','all')#" title="#objRSS.rss.channel.item[x].title.xmltext#" target="_blank" class="newsfeed">[view article in new window]</a><br />
    <br />
    </cfoutput>
    </cfloop>
    
    <!--- loop through array to output source --->
    <hr />
    <blockquote style="text-align:center;">
    <cfset SourceLength = arraylen(objRSS.rss.channel)>
    <cfloop index="x" from="1" to="#SourceLength#">
    <cfoutput>
    &copy; #objRSS.rss.channel[x].copyright.xmltext#<br />
    <em class="smallfont">(#objRSS.rss.channel.item[x].pubdate.xmltext#)</em><br />
    <br />
    <!--
    <a href="#replace(objRSS.rss.channel[x].link.xmltext,'&','&amp;','all')#" title="[visit topix.net in new window]" target="_blank"><img src="#objRSS.rss.channel[x].image.url.xmltext#" hspace="0" vspace="3" border="0" alt="#replace(objRSS.rss.channel[x].link.xmltext,'&','&amp;','all')#" /></a><br />
     -->
    <a href="#replace(objRSS.rss.channel[x].link.xmltext,'&','&amp;','all')#" title="[visit topix.net in new window]" target="_blank">[visit topix.net in new window]</a><br />
    <br />
    </cfoutput>
    </cfloop>
    </blockquote>
    
    
    <!--- show message if no updates --->
    <cfif ItemsDisplayed EQ 0>
    <cfoutput>
    No articles currently available.<br />
    <br />
    </cfoutput>
    </cfif>

<!--- catch errors --->
<cfcatch type="any">
<cfoutput>
The connection to the #ucase(url.pullnewsfrom)#'s RSS feed has timed out - please try again later.  We are sorry for any inconvenience this may have caused.<br />
<br />
</cfoutput>
</cfcatch>
</cftry>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.