3

これは、stackoverflow に関する私の砦の投稿です。このサイトで似たような Q&A をたくさん検索しましたが、私の条件は少し違うようです。ここに私のvbscriptコードがあります:

- - - - - - コードスニペット - - - - - - - -

xmlurl = "songs.xml"

set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML(xmlurl)

if xmlDoc.parseError.errorcode<>0 then
  'error handling code
  msgbox("error! " & xmlDoc.parseError.reason)
end if

------------ 終了コードスニペット ---------------

XML:

<?xml version="1.0" encoding="UTF-8"?>
<nowplaying-info-list>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771946" type="track">
    <property name="track_artist_name"><![CDATA[CKOI]]></property>
    <property name="cue_title"><![CDATA[HITMIX]]></property>
  </nowplaying-info>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771364" type="track">
    <property name="track_artist_name"><![CDATA[AMYLIE]]></property>
    <property name="cue_title"><![CDATA[LES FILLES]]></property>
  </nowplaying-info>
  <nowplaying-info mountName="CKOIFMAAC" timestamp="1339771149" type="track">
    <property name="track_artist_name"><![CDATA[MIA MARTINA]]></property>
    <property name="cue_title"><![CDATA[TOI ET MOI]]></property>
  </nowplaying-info>
</nowplaying-info-list>

UTF-8がWindowsと互換性がない可能性がある場合に備えて、最初の行を削除しようとしましたが(これに関するいくつかの投稿を見ました)、それでも同じエラーが発生しました。また、キャリッジ リターンの問題 (xml に隠された文字が埋め込まれている) があった場合に備えて、unix2dos を試してみました。私は何が間違っているのか理解できないようです。これは単純な XML ファイルです。perl 正規表現を使用して数分で解析できましたが、このスクリプトを Windows で実行する必要があるため、vbscript を使用します。同じ手法を使用して、他のソースからの XML を問題なく解析します。残念ながら XML を変更できません。外部ソースからのものです。Windows Vista ホーム エディションと Windows Server 2008 の両方でまったく同じエラーが発生しました。これまでのところ、テストのためにコマンド ラインから vbscript を実行しています (つまり、ASP ではありません)。

前もって感謝します、

サム

4

2 に答える 2

3

xmlDoc.loadXML()XML 文字列をロードできます。URL を取得できません。

HTTP 要求を行う必要がある場合は、XMLHTTPRequest オブジェクトを使用します。

Function LoadXml(xmlurl)
  Dim xmlhttp

  Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
  xmlhttp.Open "GET", xmlurl, false

  ' switch to manual error handling
  On Error Resume Next

  xmlhttp.Send
  If err.number <> 0 Then 
    WScript.Echo xmlhttp.parseError.Reason
    Err.Clear
  End If 

  ' switch back to automatic error handling 
  On Error Goto 0

  Set LoadXml = xmlhttp.ResponseXml
End Function

のように使う

Set doc = LoadXml("http://your.url/here")
于 2012-06-16T09:22:11.407 に答える
2

3つの補足事項:

(1) .parseError.reason はわかりにくい傾向があるため、その .srcTxt プロパティ (および .loadXml へのパラメーター) を含めることをお勧めします。

  Dim xmlurl : xmlurl = "song.xml"
  Dim xmlDoc : Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  xmlDoc.async = False
  xmlDoc.loadXML xmlurl
  If 0 <> xmlDoc.parseError.errorcode Then
     WScript.Echo xmlDoc.parseError.reason, "Src:", xmlDoc.parseError.srcText
  Else
     WScript.Echo "surprise, surprise"
  End if

出力:

Invalid at the top level of the document.
 Src:  song.xml

もちろん、.parseError のすべてのプロパティを考慮してそれを常に使用する Function/Sub を作成すると、さらに優れたものになります。

(2) ファイルまたは URL をロードするには、.load を使用します。

  Dim xmlDoc : Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  Dim xmlurl
  For Each xmlurl In Array("song.xml", "http://gent/~eh/song.xml", "zilch")
    xmlDoc.async = False
    if xmlDoc.load(xmlurl) Then
       With xmlDoc.documentElement.firstChild
          WScript.Echo    xmlurl _
                       , .tagName _
                       , .firstChild.tagName _
                       , .firstChild.text
       End With
    Else
       WScript.Echo xmlurl, xmlDoc.parseError.reason, "Src:", xmlDoc.parseError.srcText
    End if
  Next

出力:

song.xml nowplaying-info property CKOI-ÄÖÜ
http://gent/~eh/song.xml nowplaying-info property CKOI-ÄÖÜ
zilch The system cannot locate the object specified.
 Src:

(3) DOM を使用すると、すべてのエンコーディングの問題が回避され (そのため、ドイツ語のウムラウトを「あなたの」ファイルに入れています。これは、DOS ボックスの出力にも使用されます)。

于 2012-06-16T23:17:11.023 に答える