使用 MSXML 以 UTF-8 格式保存 XML
我正在尝试加载一个简单的 Xml 文件(以 UTF-8 编码):
<?xml version="1.0" encoding="UTF-8"?>
<Test/>
并在 vbscript 中使用 MSXML 保存它:
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
xmlDoc.Load("C:\test.xml")
xmlDoc.Save "C:\test.xml"
问题是,MSXML 以 ANSI 而不是 UTF-8 保存文件(尽管原始文件以 UTF-8 编码) 8).
MSXML 的 MSDN 文档 说 save( ) 将以 XML 定义的任何编码写入文件:
字符编码基于 XML 声明中的编码属性,例如 .当未指定编码属性时,默认设置为 UTF-8。
但这显然至少在我的机器上不起作用。
MSXML如何保存为UTF-8?
I'm trying to load a simple Xml file (encoded in UTF-8):
<?xml version="1.0" encoding="UTF-8"?>
<Test/>
And save it with MSXML in vbscript:
Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
xmlDoc.Load("C:\test.xml")
xmlDoc.Save "C:\test.xml"
The problem is, MSXML saves file in ANSI instead of UTF-8 (despite the original file being encoded in UTF-8).
The MSDN docs for MSXML says that save() will write the file in whatever encoding the XML is defined in:
Character encoding is based on the encoding attribute in the XML declaration, such as . When no encoding attribute is specified, the default setting is UTF-8.
But this is clearly not working at least on my machine.
How can MSXML save in UTF-8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 MSXML 中的另外两个类将正确编码的 XML 写出到输出流。
这是我写入通用 IStream 的辅助方法:
为了保存到文件,我使用 FileStream 调用 Stream 版本:
您可以将函数转换为任何语言你喜欢。这些是德尔福。
You use two other classes in MSXML to write out XML properly encoded to an output stream.
Here is my helper method that writes to a generic IStream:
In order to save to a file i call the Stream version with a FileStream:
You can convert the functions to whatever language you like. These are Delphi.
XML 文件中没有任何非 ANSI 文本,因此无论是 UTF-8 还是 ASCII 编码,它都是相同的。在我的测试中,将非 ASCII 文本添加到 test.xml 后,MSXML 始终以 UTF-8 编码保存,并且如果一开始就有 BOM,也会写入 BOM。
http://en.wikipedia.org/wiki/UTF-8
http://en.wikipedia.org/wiki/Byte_order_mark
There isn't any non-ANSI text in your XML file, so it will be identical whether UTF-8 or ASCII encoded. In my tests, after adding non-ASCII text to test.xml, MSXML always saves in UTF-8 encoding and also writes the BOM if there was one to begin with.
http://en.wikipedia.org/wiki/UTF-8
http://en.wikipedia.org/wiki/Byte_order_mark
当您执行
load
时,msxml 不会将处理指令中的编码复制到创建的文档中。所以它不包含任何编码,看起来 msxml 选择了它喜欢的东西。在我的环境中,它是我不喜欢的 UTF-16。解决方案是提供处理指令并在那里指定编码。如果您知道该文档没有处理指令,则代码很简单:
如果该文档可能包含其他处理指令,则必须首先将其删除(因此下面的代码必须位于上面的代码之前)。我不知道如何使用
selectNode
来做到这一点,所以我只是迭代了所有根节点:抱歉,如果代码没有直接执行,因为我修改了工作版本,这是这样写的在自定义的东西中,而不是 vbscript 中。
When you perform
load
msxml does not copy encoding from the processing instruction into the created document. So it doesn't contain any encoding and seems like msxml chooses something which it likes. In my environment it's UTF-16 which I don't prefer.The solution is to provide processing instructions and specify encoding there. If you know that the document has no processing instructions, the code is trivial:
If it's possible, that the document contained other processing instruction, it must be removed first (so the code below must come before the code above). I don't know how to use
selectNode
to do it, so I just iterated all root nodes:Sorry if the code doesn't execute directly, because I modified working version, that was written in something custom, not vbscript.
此解决方法似乎有效:将 xml 字符串传递给 ADODB.Stream 以保存文件。
更新:处理指令按照以下方式丢失:
This workaround seems to work: passing the xml string to ADODB.Stream to save the file.
Update: the processing instruction get lost according to this: