Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Agreed