ColdFusion XmlParse() Error - Content Is Not Allowed In Prolog

Posted September 25, 2006 at 12:04 PM by Ben Nadel

Tags: ColdFusion

I was working on an XML database this weekend and was stuck for a bit on an error getting thrown during an XmlParse() call. The error was:

Content is not allowed in prolog.

The original code was trying to take an XML document object, convert it into a string and then parse it into a ColdFusion query object (for testing). That's where the XmlParse() method call came into play.

  • <!---
  • Try to convert the xml object to an xml string then
  • into a query object the using XmlToQuery() UDF.
  • --->
  • <cfset qData = XmlToQuery( xmlDoc.ToString() ) />

I thought it was a white space problem prior to the XML doctype. I have run into that problem before. I tried to Trim() the content before I sent it in, but that did not help. The issue, I finally worked out, was the "ToString()" method call. I was calling it incorrectly - I always do that! You can't call the method from the XML document object. You have to call it PASSING IN the Xml document:

  • <!---
  • Try to convert the xml object to an xml string then
  • into a query object using the XmlToQuery() UDF.
  • --->
  • <cfset qData = XmlToQuery( ToString( xmlDoc ) ) />

I finally figured this out because dumping out xmlDoc.ToString() resulted in:

#document: null

... which obviously was NOT a string representation of the XML document object.




Reader Comments

Kai
Jan 2, 2007 at 2:16 AM // reply »
1 Comments

Actually - this used to work fine in CF 6, but not anymore in CF 7: http://www.bloginblack.de/archives/000749.cfm


Feb 15, 2007 at 1:37 PM // reply »
3 Comments

I am also having a problem with getting a [#document: null] when attempting to 'save' the xml document to disk -
When i cfdump the xml object it looks fine - (cfmx 7).
But after I do a xmldoc.ToString() on it - the text it saves is the [#document: null].

How can I convert the created XML doc to a String (I had to do some 'funky' things prior to saving this xml doc, like merging it etc - so maybe that would be the cause, but either way, it renders fine in cfdump but I can't get it back to it's String representation.

ideas?


Feb 15, 2007 at 1:40 PM // reply »
3 Comments

Ah - ok - I dind't realize toString() could be used like that -
so I did this toString(myxmldoc) and all is good now -

Thanks!


Feb 15, 2007 at 1:43 PM // reply »
10,640 Comments

Kevin,

Instead of doing XML.ToString()... rock ToString( XML ). Give that a try.


May 13, 2007 at 9:18 PM // reply »
2 Comments

I realize that this is a posting from a *long* time ago (I added myself to the "use it or lose it" e-mail), but I have run across this error before also. It turned out to be a problem within a webservice that I was writing, but had nothing to do with the "tostring" or with any whitespace before the XML...it just was throwing an error within the code itself, and, because the error was being thrown, was appearing to CF as if there was whitespace before the XML. I guess it makes sense, but it would have been nice if the actual error was thrown also :)


May 14, 2007 at 7:12 AM // reply »
10,640 Comments

That's good to know. I don't have much experience with Web Services, so it's good for me to know where possible errors are coming from. But I agree, some of the errors that ColdFusion throws are not exactly the most illuminating.


Feb 15, 2008 at 8:45 AM // reply »
5 Comments

Im not using toString, but am getting the same error, occasionally. Thats right occasionally! My webservice works 99% of the time, then I get these odd errors. At first I contributed it to the feed coming back in error, but then my isXML check would have caught that. So as of now, Im stumped.


Feb 15, 2008 at 9:58 AM // reply »
4 Comments

In my experiences working with XML, i find that every once in a while, no matter if we cdata nodes or not - some data we get from xml can have high ascii characters in them - which may bomb out at the parse level. The xml may parse just fine in Internet Explorer or Firefox, but when that same xml is given to cf to xmlparse, it will throw an error. Removing of the often hidden character or character that appears in notepad as a 'box' character often fixes the problem -


Feb 15, 2008 at 10:31 AM // reply »
10,640 Comments

Kevin has a good point. In addition to that, make sure that you are using XmlFormat() even for the content that is inside of CDATA[]. You might have to unescape somethings afterwards, though. However, it might help you to narrow down where the problem is.

If its a high-ascii value, I was just blogging on the topic of stripping out ascii values:

http://www.bennadel.com/index.cfm?dax=blog:1155.view


Aug 14, 2008 at 10:40 AM // reply »
9 Comments

This error can also be caused by the BOM (byte order mark) character in the XML feed. I blogged about it last time I encountered this problem: http://www.stillnetstudios.com/2007/07/09/coldfusion-xml-prolog-error


Sep 25, 2008 at 12:57 PM // reply »
3 Comments

trim(), extra #, blah blah blah... didn't work but the below works for me:

in CF Admin >> setting >> enable white space management


Sep 25, 2008 at 1:11 PM // reply »
3 Comments

take that back....not working my eyes cheated me, only partial better coz at least the xml showing properly now, but still getting that error.


Sep 25, 2008 at 2:55 PM // reply »
3 Comments

okay, fixed... if you are developing and can get hold of the xml try creating an empty file with notepad called test.txt. Copy and paste the xml into this .txt and change it into .xml.

You will notice there is an extra 1 byte of code that you can't see that is causing this error.


Apr 2, 2009 at 4:16 PM // reply »
26 Comments

I want to also add, that you can get this if you are doing a cfhttp request to a URL that you expect to return XML, but returns something like a 'Connection Failure' or some other invalid data.

I just ran into this - and depending on your environment, can be quite tricky to debug as it may be a network configuration thing and not a code issue at all.


Apr 3, 2009 at 8:15 AM // reply »
10,640 Comments

@Kevin,

Good point, thanks.


Jan 20, 2010 at 4:01 PM // reply »
7 Comments

Hey guys,

I was getting this error today about the prolog. I was trying to do something like:

<cfhttp url="http://maps.google.com/maps/geo?q=#address#&output=xml&key=#GMAP_KEY#" />

<cfset myXml = XmlParse(cfhttp.FileContent)>

The problem was occurring because there was a "#" symbol in the street address.

If anybody does something like this, I would recommend replacing the "#" symbol like this:

<cfset address = '#Replace(address, "##", "", "ALL")#'>

The prolog error went away and now everything is back to normal.

Sincerely,
Travis Walters


Jan 20, 2010 at 10:53 PM // reply »
10,640 Comments

@Travis,

I am not sure I completely follow you; are you saying that the XmlParse() method call was throwing an error because there was a # symbol in the XML string?


Jan 21, 2010 at 12:16 AM // reply »
7 Comments

@Ben Nadel,

When the "#" symbol is included in a street address that get sent to the Google server, the cfhttp.FileContent comes back in a strange format that the XMLParse does not interpret correctly.

Error Example:

<cfhttp url="http://maps.google.com/maps/geo?q=2017 Mission Street ##303,San Francisco,California,94110,US&output=xml&key=#GMAP_KEY#" />

<cfset myXml = XmlParse(cfhttp.FileContent)>

Solution:

<cfhttp url="http://maps.google.com/maps/geo?q=2017 Mission Street 303,San Francisco,California,94110,US&output=xml&key=#GMAP_KEY#" />

The error given in the first example is the prolog error. Just thought I would warn people about this glitch I encountered that seemed to go well with your article.

Sincerely,

Travis Walters


Jan 21, 2010 at 9:36 PM // reply »
10,640 Comments

@Travis,

Thanks for clarifying. Looks like they might be sending back a corrupt result of some sort.


Max
May 13, 2010 at 4:46 PM // reply »
4 Comments

I was having the same issue and this didn't work for me. What did work for me was to call in the xml using cffile:

<cffile action="read" file="#expandPath('pathto/my.xml')#" variable="myxml">
<cfset qData = xmlParse(myxml) />


May 13, 2010 at 5:57 PM // reply »
10,640 Comments

@Max,

How were you reading the XML in before you went the CFFile route?



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »