Jas*_*son 3 xml coldfusion encoding
我正在使用XMLFormat()来编码XML文档的一些文本.但是,当我去读取我创建的XML文件时,我得到一个无效的字符错误.为什么XMLFormat()没有正确编码所有字符?
我正在运行CF8.
您确定以正确的编码输出文件吗?你不能这样做
<cffile action="write" file="foo.xml" output="#xml#" />
Run Code Online (Sandbox Code Playgroud)
因此结果很可能与XML所在的字符集不同.除非另有说明(通过编码声明),否则XML文件将被视为UTF-8,您应该:
<cffile action="write" file="foo.xml" output="#xml#" charset="utf-8" />
<!--- and --->
<cffile action="read" file="foo.xml" variable="xml" charset="utf-8" />
Run Code Online (Sandbox Code Playgroud)
我觉得这是XMLFormat中的一个错误.我不确定下面的片段的原始作者是谁,但这里是通过正则表达式捕获额外字符的方法...
<cfset myText = xmlFormat(myText)>
<cfscript>
i = 0;
tmp = '';
while(ReFind('[^\x00-\x7F]',myText,i,false))
{
i = ReFind('[^\x00-\x7F]',myText,i,false); // discover high chr and save it's numeric string position.
tmp = '&##x#FormatBaseN(Asc(Mid(myText,i,1)),16)#;'; // obtain the high chr and convert it to a hex numeric chr.
myText = Insert(tmp,myText,i); // insert the new hex numeric chr into the string.
myText = RemoveChars(myText,i,1); // delete the redundant high chr from string.
i = i+Len(tmp); // adjust the loop scan for the new chr placement, then continue the loop.
}
return myText;
</cfscript>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5120 次 |
| 最近记录: |