Search code examples
xmlcoldfusionrsscoldfusion-8

Element undefined in coldfusion.xml.XmlNodeMap


I am trying to load these RSS feeds from the NY Times:

When I use the following line of code, everything loads fine, no errors.

     <a href="#XMLContent.rss.channel.item[idx].link.xmlText#">
        #XMLContent.rss.channel.i‌​tem[idx].title.xmlText#
     </a> 

BUT when I actually click on the links, the URLs did not come over. I had a look at the <cfdump> and saw that for these feeds, the URLs are held in the ID and rsslink fields. When I pull the feed, using either of one of these:

    <a href="#XMLContent.rss.channel.item[idx].id.xmlText#">
         #XMLContent.rss.channel.ite‌m[idx].title.xmlText#
    </a>
    <a href="#XMLContent.rss.channel.item[idx].rsslink.xmlText#">
        #XMLContent.rss.channe‌l.item[idx].title.xmlText#
    </a> 

I get one of these errors:

Element RSSLINK.XMLTEXT is undefined in a Java object of type class coldfusion.xml.XmlNodeMap referenced as ''

Element RSSLINK.XMLTEXT is undefined in a Java object of type class coldfusion.xml.XmlNodeMap referenced as ''

Does anyone know how to get rid of this error? I have googled but to no avail.


Solution

  • I am not sure where you getting "ID" and "rsslink". From what I can see, the "link" url is stored in an attribute named "href". Try using XmlAttributes:

    #XMLContent.rss.channel.item[idx].link.XmlAttributes['href']#
    

    Update:

    I ran a few tests under CF8, and using the attribute worked fine (see below). Note, you did not include the code you are using to parse the feed, so I guessed you are using cffhtp in combination with xmlParse.

    <cfhttp url="http://rss.nytimes.com/services/xml/rss/nyt/Politics.xml" result="rssXML">
    <cfset XMLContent = xmlParse(rssXML.fileContent)>
    
    <cfloop from="1" to="#arrayLen(XMLContent.rss.channel.item)#" index="idx">
        <cfset itemNode = XMLContent.rss.channel.item[idx]>
        <cfoutput>
            <a href="#itemNode.link.XmlAttributes['href']#">
                #itemNode.title.xmlText#
            </a><br />
        </cfoutput>
    </cfloop>
    

    Note: The xml structure on the business feed seems to vary. Some of the nodes store the url in the href attribute and others in link.xmlText. XMLAttributes is a structure. So you can always use the structKeyExists function to verify the href attribute exists before using it. Then do something else if it is not found.

        <cfif structKeyExists(itemNode.link.XmlAttributes, "href")>
            attributes exists. go ahead and use it
        <cfelse>
            not found. do something else ...
        </cfif>