Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Use Of XmlFormat() in ColdFusion - 9

Participant ,
Jun 21, 2012 Jun 21, 2012

Hi All,

I found that XmlFormat() function does the formatting of the any Xml text and removes any XML unsafe characters from that XML text.

I just wrote below code to test this function.

<cfsavecontent variable="x" >

<?xml version = "1.0"?> 

<someXML>

    <someElement someAttribute="'a quoted value'"> 

       Body of element with <, >, "" and & goes here.

    </someElement>

</someXML>

</cfsavecontent>

<cfset x = XmlFormat(x) />

<cfscript>

          y = XmlParse(trim(x));

          writeDump(y);

</cfscript>

Then I got another error message

An error occured while Parsing an XML document.

Reference is not allowed in prolog.

I know that by replacing the special characters from the XML text by string replace functions everything will be fine. But, I want to know what is the exact use of XmlFormat() function.

Thanks in advance for any help.

2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 21, 2012 Jun 21, 2012

You don't encode the entire document. Only the pieces that should be encoded.

<cfsavecontent variable="x" >

<?xml version = "1.0"?>

<someXML>

    <someElement someAttribute="'a quoted value'">

       #xmlFormat("Body of element with <, >, "" and & goes here.")#

    </someElement>

</someXML>

</cfsavecontent>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 21, 2012 Jun 21, 2012

Thanks for the quick response.

I can understand what you are saying. Suppose I am getting the result from one HTTP call and which contains some special characters in the XML text. How could I do it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 21, 2012 Jun 21, 2012

If the XML is already constructed without proper encoding then you'll have a hard time getting it encoded properly.  You could try parsing it and reconstructing it with proper encoding, but if it has characters that interfere with the parsing, then you are hosed.

jason

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 21, 2012 Jun 21, 2012

You could try parsing it and reconstructing it with proper encoding,

Does the above line means Xml Parsing or regular expression Parsing of Xml string. Can you please explain your idea?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 22, 2012 Jun 22, 2012

If the XML is already constructed without proper encoding then you'll have a hard time getting it encoded properly.  You could try parsing it and reconstructing it with proper encoding, but if it has characters that interfere with the parsing, then you are hosed.

The first thing I would do is to get in touch with whoever is providing the XML and let them know it's malformed.  They might just fix it at their end.  This would be the best (initial ~) approach.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 22, 2012 Jun 22, 2012
LATEST

Agreed

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources