1

「XMLDOMDocument に XML 宣言を含めるにはどうすればよいですか?」で見られるコードのバリアントを使用しています。(これはMSDNでも見ることができます。エンコーディングを「UTF-16」に変更すると、UTF-16 として出力されると思われます...そして、テキスト エディターで出力を見ると「そうします」... ; しかし、16 進エディタでチェックすると、(プロパティが true に設定されているにもかかわらず) バイト オーダー マークが欠落しており、XML エディタは BOM が欠落しているためにドキュメントを無効な UTF-16 として拒否します。

私は何を見落としていますか?

'' # Create and load a DOMDocument object.

Dim xmlDoc As New DOMDocument60
xmlDoc.loadXML("<doc><one>test1</one><two>test2</two></doc>")

'' # Set properties on the XML writer - including BOM, XML declaration and encoding

Dim wrt As New MXXMLWriter60
wrt.byteOrderMark = True
wrt.omitXMLDeclaration = False
wrt.encoding = "UTF-16"
wrt.indent = False

'' # Set the XML writer to the SAX content handler.

Dim rdr As New SAXXMLReader60
Set rdr.contentHandler = wrt
Set rdr.dtdHandler = wrt
Set rdr.errorHandler = wrt
rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt

'' # Now pass the DOM through the SAX handler, and it will call the writer

rdr.parse xmlDoc

'' # Let the writer do its thing

Dim iFileNo As Integer
iFileNo = FreeFile
Open App.Path + "\saved.xml" For Output As #iFileNo
Print #iFileNo, wrt.output
Close #iFileNo

出力は次のようになります。

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<doc><one>test1</one><two>test2</two></doc>

なぜ VB6 を使用しているのですか? 実際には VBA (同世代、VB6 のわずかなサブセット) であり、EMC-Captiva の InputAccel/FormWare のスクリプト言語として使用されるため、切り替えはオプションではありません。

4

1 に答える 1

2

問題は、ライターの出力プロパティから値を取得すると、文字列が取得されることです。VB の文字列は常に UTF-16 であるため、エンコーディングに関係なく取得できます。VB では文字列は常に UTF-16 であるため、BOM が必要であるという概念はないため、BOM も含まれません。

エンコーディングと BOM プロパティは、出力プロパティに IStream の実装が割り当てられている場合にライターが XML を書き込む方法にのみ影響します。

次のように、parse への呼び出しの周りのコードを変更してみてください:-

Dim oStream As ADODB.Stream
Set oStream =  New ADODB.Stream
oStream.Open
oStream.Type = adTypeBinary

wrt.output = oStream

rdr.parse xmlDoc

oStream.SaveToFile App.Path + "\saved.xml"
oStream.Close

これにより、目的の出力が生成されます。

于 2009-12-08T18:35:21.503 に答える