coldfusioncoldfusion-8

An invalid XML character (Unicode: 0x1e) in Coldfusion XML output


I am attempting to output a query to a simple XML document. I have used this same code multiple times for other queries and it worked fine. It seems there is a bad character in the description field somewhere and XMLformat() is not filtering it out. I have tried numerous REReplace() filters to no avail. Also tried Ben Nadel's technique found here. http://www.bennadel.com/blog/1155-Cleaning-High-Ascii-Values-For-Web-Safeness-In-ColdFusion.htm and everything has resulted in the same error. I did dump the output and search for bad characters and I found nothing. This simple thing has turned into quite the mystery. I am using the code below.

<cfquery name="list" datasource="theDatasource">
SELECT ItemID, ItemCode, BrandName, description
FROM theTable
</cfquery>

<cfxml variable="outputXML">
<itemsBrand>
    <cfoutput query="list">
    <itemBrand>
        <ItemID>#XmlFormat(ItemID)#</ItemID>
        <ItemCode>#XmlFormat(ItemCode)#</ItemCode>
        <BrandName>#XmlFormat(BrandName)#</BrandName>
        <description>#XmlFormat(description)#</description>
    </itemBrand>
    </cfoutput>
</itemsBrand>
</cfxml>

<cffile action="write" file="#GetDirectoryFromPath(GetCurrentTemplatePath())#itemBrand.xml" output="#toString(outputXML)#"/>

Is resulting in the error "An invalid XML character (Unicode: 0x1e) was found in the element content of the document." Any help would be appreciated.


Solution

  • It seems a bit odd it's barfing on a 0x1E, which is just a greater-than symbol. (edit: no it ain't; not sure why I came to that conclusion. AC).

    It might be an idea to swap out the <cfxml> for <cfsavecontent> temporarily so you can build the string, then parse it to find out what's not right about it. That should give you more clue how to sort it out. But xmlFormat() is supposed to deal with wayward angle-brackets.

    One other thing: if all you're doing with this XML is to then serialise it and write it to file, you don't actually need to use <cfxml> anyhow. Just continue to use <cfsavecontent> anyhow. If you want a string: just make a string.