Skip navigation
Currently Being Moderated

Use Of XmlFormat() in ColdFusion - 9

Jun 21, 2012 7:08 PM

Tags: #cf9 #xmlformat

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.

 
Replies
  • Currently Being Moderated
    Jun 21, 2012 7:25 PM   in reply to Upen@Roul

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2012 8:35 PM   in reply to Upen@Roul

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 12:31 AM   in reply to 12Robots

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 9:13 AM   in reply to Adam Cameron.

    Agreed

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points