如何使 TXMLDocument(使用 MSXML 实现)始终包含编码属性?
我有旧代码(我没有编写它)始终包含编码属性,但将其重新编译为 D2010,TXMLDocument 不再包含编码。由于 XML 数据在标签和数据上都有重音字符,因此 TXMLDocument.LoadFromFile 只是抛出 EDOMParseErros,表示在文件中发现了无效字符。相关代码:
Doc := TXMLDocument.Create(nil);
try
Doc.Active := True;
Doc.Encoding := XMLEncoding;
RootNode := Doc.CreateElement('Test', '');
Doc.DocumentElement := RootNode;
<snip>
//Result := Doc.XMl.Text;
Doc.SaveToXML(Result); // Both lines gives the same result
在旧版本的 Delphi 上,生成以下行:
<?xml version="1.0" encoding="ISO-8859-1"?>
在 D2010 上,生成以下行:
<?xml version="1.0"?>
如果我手动更改该行,则所有工作都像过去几年一样工作。
更新:XMLEncoding 是一个常量,定义如下
XMLEncoding = 'ISO-8859-1';
I have legacy code (I didn't write it) that always included the encoding attribute, but recompiling it to D2010, TXMLDocument doesn't include the encoding anymore. Because the XML data have accented characters both on tags and data, TXMLDocument.LoadFromFile simply throws EDOMParseErros saying that an invalid character is found on the file. Relevant code:
Doc := TXMLDocument.Create(nil);
try
Doc.Active := True;
Doc.Encoding := XMLEncoding;
RootNode := Doc.CreateElement('Test', '');
Doc.DocumentElement := RootNode;
<snip>
//Result := Doc.XMl.Text;
Doc.SaveToXML(Result); // Both lines gives the same result
On older versions of Delphi, the following line is generated:
<?xml version="1.0" encoding="ISO-8859-1"?>
On D2010, this is generated:
<?xml version="1.0"?>
If I change manually the line, all works like always worked in the last years.
UPDATE: XMLEncoding is a constant and is defined as follow
XMLEncoding = 'ISO-8859-1';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要查看 IXMLDocument.CreateProcessingStruction。我使用 OmniXML,但它的语法类似,应该可以帮助您入门:
You'll want to see IXMLDocument.CreateProcessingStruction. I use OmniXML, but it's syntax is similar and should get you started:
由于 Ken 的回答和 MSXML 文章的链接,我决定研究 XML 属性和 SaveToXML 方法。两者都使用 MSXMLDOM 实现的 XML 属性 - 在文章中据说直接读取时不带编码(在使用 CreateProcessInstruction 方法之后的“使用 MSXML 创建新的 XML 文档”部分中)。
更新:
我发现重音字符在生成的 XML 中被截断。当该 XML 的处理器开始抛出奇怪的错误时,我们看到字符正在转换为数字字符常量(#13 是用于回车的数字字符常量)。因此,我使用 TStringStream 终于成功了。
Since Ken's answer and the link to MSXML article, I decided to investigate the XML property and SaveToXML method. Both use the XML property of the MSXMLDOM implementation - which in the article is said that do not bring the encoding when directly read ( in the "Creating New XML Documents with MSXML" section right after the use of CreateProcessInstruction method).
UPDATE:
I found that accented characters are getting truncated in the resulting XML. When the processor of that XML started to throw strange errors, we saw that the chars are being converted to the numeric char constant ( #13 is the numeric char constant for carriage return). So, I used a TStringStream to get it FINALLY right.