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

Posted September 25, 2006 at 12:04 PM

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.

 Launch code in new window » Download code as text file »

  • <!---
  • 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:

 Launch code in new window » Download code as text file »

  • <!---
  • 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.

Download Code Snippet ZIP File

Comments (9)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

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

Posted by Kai on Jan 2, 2007 at 2:16 AM


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?

Posted by Kevin Penny on Feb 15, 2007 at 1:37 PM


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

Thanks!

Posted by Kevin Penny on Feb 15, 2007 at 1:40 PM


Kevin,

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

Posted by Ben Nadel on Feb 15, 2007 at 1:43 PM


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 :)

Posted by Gareth Arch on May 13, 2007 at 9:18 PM


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.

Posted by Ben Nadel on May 14, 2007 at 7:12 AM


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.

Posted by Josh on Feb 15, 2008 at 8:45 AM


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 -

Posted by Kevin Penny on Feb 15, 2008 at 9:58 AM


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

Posted by Ben Nadel on Feb 15, 2008 at 10:31 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting