gpt4 book ai didi

asp-classic - 使用 MSXML 4.0 读取 UTF-8 XML

转载 作者:行者123 更新时间:2023-12-04 02:42:05 24 4
gpt4 key购买 nike

我在使用 classc ASP/VBScript 尝试使用 MSXML 读取 UTF-8 编码的 XML 文件时遇到问题。文件编码正确,我可以用所有其他工具看到这一点。

构造的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<itshop>
<Product Name="Backup gewünscht" />
</itshop>

如果我尝试在 ASP 中执行此操作...

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("input.xml", FOR_READING)
XML = ts.ReadAll
ts.Close
Set ts = nothing
Set fso = Nothing

Set myXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
myXML.loadXML(XML)
Set DocElement = myXML.documentElement
Set ProductNodes = DocElement.selectNodes("//Product")
Response.Write ProductNodes(0).getAttribute("Name")
' ...

... 并且 Name 包含特殊字符(具体为德语变音符号)变音符号“双字节代码”的字节被重新编码,所以我最终得到两个完全糟糕的无意义字符。应该是“ü”的变成了“¼”——在我的输出中是四个字节,而不是两个(正确的 UTF-8)或一个(ISO-8859-#)。

我做错了什么?为什么 MSXML 认为输入是 ISO-8859-# 以便尝试将其转换为 UTF-8?

最佳答案

Set ts = fso.OpenTextFile("input.xml", FOR_READING, False, True)

最后一个参数是“Unicode”标志。

OpenTextFile() 具有以下签名:

object.OpenTextFile(filename[, iomode[, create[, format]]])

其中“格式”定义为

Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

而三态定义为:

TristateUseDefault  -2   Opens the file using the system default.
TristateTrue -1 Opens the file as Unicode.
TristateFalse 0 Opens the file as ASCII.

而-1恰好是True的数值。

无论如何,更好的是:

Set myXML = Server.CreateObject("Msxml2.DOMDocument.4.0")
myXML.load("input.xml")

为什么要使用 TextStream 对象来读取 MSXML 本身可以完美读取的文件。

TextStream 对象也没有实际 文件编码的概念。文档说“Unicode”,但编码 Unicode 的方法不止一种。 MSXML 对象的 load() 方法将能够处理所有这些。

关于asp-classic - 使用 MSXML 4.0 读取 UTF-8 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/971495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com