Link to home
Start Free TrialLog in
Avatar of RickEpnet
RickEpnetFlag for United States of America

asked on

Error while Parsing an XML document (Coldfusion)

CF8 Windows 2003 IIS

I am getting this error
Content is not allowed in prolog

From this code.
 <cfhttp url="http://www.sbnation.com/ncaa-football/teams/pittsburgh-panthers/rss" method="get">
     <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
 </cfhttp>
      <cfset objRSS = xmlParse(cfhttp.filecontent)>
    <CFDUMP var="#objRSS#">

I cannot figure it out please help.
Avatar of _agx_
_agx_
Flag of United States of America image

Content is not allowed in prolog

The headers aren't quite right, so the call is actually failing. If you dump the cfhttp content the result is actually "Connection Failure" instead of valid xml. That's why your xmlParse isn't working.  By default CFHTTP doesn't throw an error if something goes wrong. So you should always check the status code to verify the call succeeded.  


The correct headers from: Workaround for CFHTTP and Compressed HTTP Response from IIS


<cfhttp url="http://www.sbnation.com/ncaa-football/teams/pittsburgh-panthers/rss"
      method="get">
      <cfhttpparam type="header" name="Accept-Encoding" value="*" />
      <cfhttpparam type="Header" name="TE" value="deflate;q=0">
</cfhttp>
<cfif cfhttp.statusCode eq "200 OK">
      <cfset objRSS = xmlParse(cfhttp.filecontent)>
      success... do something with results
<cfelse>
      call failed. do error handling ...
    <cfdump var="#cfhttp#" label="Debug results">
</cfif>
<cfif cfhttp.statusCode eq "200 OK">

fyi - status code isn't 100% bullet proof. Sometimes you get a 200 OK for failed pages too. But it's better than not checking at all.

Edit: You could also set throwonerror="true" and wrap everything in a try/catch. It may be a little more expensive, but is probably more robust than checking the status.

<cftry>
      <cfhttp url="http://www.sbnation.com/ncaa-football/teams/pittsburgh-panthers/rss"
            method="get" throwonerror="true">
            .... headers
      </cfhttp>
      <cfset objRSS = xmlParse(cfhttp.filecontent)>
      success... do something with results
      <cfcatch>
            failed .. do error handling here
      </cfcatch>
</cftry>
Avatar of RickEpnet

ASKER

I will try theses thanks!!
Although your second post works as far as the error handling. I think there should be a way to phrase the sbnation rss feeds. Any ideas.
I think there should be a way to phrase the sbnation rss feeds.

Sorry I don't understand the question.  The 1st post does parse the feed correctly. Was there some other issue?
I still get an error when I run the first Post. It is showing connection failure could it be that SBnation is blocking the connect from the web server?
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial