"The processing instruction target matching "[xX][mM][lL]" is not allowed. "
I just regenerated the error with the following code sample.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfsavecontent variable="request.x"> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<employee> | |
<!-- A list of employees --> | |
<name EmpType="Regular"> | |
<first>Almanzo</first> | |
<last>Wilder</last> | |
</name> | |
<name EmpType="Contract"> | |
<first>Laura</first> | |
<last>Ingalls</last> | |
</name> | |
</employee> | |
</cfsavecontent> | |
<cftry> | |
<cfset mydoc = XmlParse(request.x)> | |
<cfdump var="#mydoc#" label="Employee Dump"> | |
<cfcatch> | |
<cfdump var="#cfcatch#"> | |
</cfcatch> | |
</cftry> |
Then I searched in web I found that this is not related to any specific language, this is related to the XML declaration format.
According to the XML spec we can't have anything at all before the XML prolog. In our XML we are staring with XML declaration. So, that XML declaration should be the first character in our XML document no white space or any special characters are allowed before our XML document declaration.
So, the question is how we can avoid this in our above code?
- Use trim before parsing the XML string like below
<cfset mydoc = XmlParse(trim(request.x))>
So, trim function will remove any extra spaces present in the document before starting the XML declaration.
- Write XML declaration as the first character of your XML text. No white space or any other characters at the starting position.
- Remove your XML declaration from the XML text.
Hope it will save your time...
No comments:
Post a Comment