Coldfusion - formatting an XML string returned from an API call

We call an API that returns a string of XML-formatted data. We would like to convert this string to a ColdFusion XML object through XMLParse (). A problem occurs when special characters are displayed in data values. For example, such characters:

  — –

(yes, the source data contains them in an equivalent encoded in HTML). When executing XMLParse (), it generates an error for these encoded characters. Here is an example that will result in an error:

Part of our line: <event>Hello &nbsp; World</event>

Mistake: Reference to undefined entity "&nbsp;"

What is the best way to make these characters compatible with XMLParse ()? And more importantly, how can we do this if we do not always know what the symbols will be?

Thanks!

(this is on ColdFusion 6 server)

+3
7

:

ReplaceList(xml, "&nbsp;,&mdash;,&ndash;", "#Chr(160)#,#Chr(8212)#,#Chr(8211)#")

, , char. Chr() , , .

+2
replace(xml, '&','&amp;','all');

. DTD , , , , - , , .

+1

: HTML ColdFusion?

, XmlFormat(), XmlParse().

"nbsp 5 ", @stevenerat.

+1

Replacing "&" with "&amp;"and then again after parsing seems to work

<cfsavecontent variable="xmlString">
    <event>Hello&nbsp;World&amp;</event>
</cfsavecontent>
<cfset xmlString = Replace(xmlString, "&", "&amp;", "all") />
<cfset doc = Xmlparse(xmlString) />
<cfset value = Replace(doc.event.xmlText, "&amp;", "&","all") />
0
source

You can take a look at XmlFormat () . Easy to use:

<cfset string = XmlFormat(string)>
-2
source

Source: https://habr.com/ru/post/1721899/


All Articles