2011-08-30 77 views
2

here: XML:如何使用vbscript和經典asp在xml中選擇SingleNode?

<Vocabulary> 
    <Word type="noun" level="1"> 
     <English>cat</English> 
     <Spanish>gato</Spanish> 
    </Word> 
    <Word type="verb" level="1"> 
     <English>speak</English> 
     <Spanish>hablar</Spanish> 
    </Word> 
    <Word type="adj" level="1"> 
     <English>big</English> 
     <Spanish>grande</Spanish> 
    </Word> 
</Vocabulary> 

我創建的XML文件把它放在同一個目錄中經典的asp文件:

<% 

    Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
    objXMLDoc.async = False 
    objXMLDoc.load("vocabulary.xml") 

    Set Node = objXMLDoc.documentElement.selectSingleNode("Word/Spanish") 
    document.write(Node.text) 

%> 

但我得到這個:

微軟的VBScript運行時錯誤「800a01a8」

所需對象:'objXMLDoc.documentElement'

/so-rms/reports/xmltest.asp,7號線

我在做什麼錯?他們得到的元素。我收到錯誤。謝謝。

編輯:我把這個在:

If objXMLDoc.parseError.errorCode <> 0 Then 
    response.write objXMLDoc.parseError.errorCode & "ERROR CODE </br>" 
    response.write objXMLDoc.parseError.reason & "REASON CODE </br>" 
    response.write objXMLDoc.parseError.line & "LINE CODE </br>" 
End If 

,並得到:

-2146697210ERROR CODE

系統錯誤:-2146697210。原因碼

0LINE CODE 從下面的嘗試:

dim path: path = Server.MapPath("vocabulary.xml") 
dim fso: set fso = CreateObject("Scripting.FileSystemObject") 
if not fso.FileExists(path) then 
    Response.Write "path '" & path & "' not found" 
end if 
Set objXMLDoc = CreateObject("MSXML2.DOMDocument.3.0") 
objXMLDoc.async = False 

if not objXMLDoc.load("vocabulary.xml") then 
    ' report loading error 
    response.write "error" 
end if 
'objXMLDoc.load("vocabulary.xml") 
If objXMLDoc.parseError.errorCode <> 0 Then 
    response.write objXMLDoc.parseError.errorCode & "ERROR CODE </br>" 
    response.write objXMLDoc.parseError.reason & "REASON CODE </br>" 
    response.write objXMLDoc.parseError.line & "LINE CODE </br>" 
End If 
Set Node = objXMLDoc.documentElement.selectSingleNode("Word/Spanish") 
document.write(Node.text) 

編輯:

我也改變了xml文件工作XML返回的URL(Bing地圖)和它的工作。所以我猜這是文件。謝謝。

+0

你試過'objXMLDoc.load(路徑)',其中'path'是調用'MapPath()'的結果? – 2011-08-30 18:22:08

回答

4

我認爲你的xml文檔沒有加載。該load()方法返回一個bool指示文件是否已正確加載,所以你可以檢查

if not objXMLDoc.load("vocabulary.xml") then 
    ' report loading error 
end if 

parseError也酒店有srcText屬性,你可以用它來確定在何處解析問題發生的文件中。

檢查文件是否存在於您正在使用的路徑中也是一個好主意。使用Server.MapPath()Scripting.FileSystemObject做到這一點:

dim path: path = Server.MapPath("vocabulary.xml") 
dim fso: set fso = CreateObject("Scripting.FileSystemObject") 
if not fso.FileExists(path) then 
    Response.Write "path '" & path & "' not found" 
end if 

此外,我建議使用XML庫的更新版本,MSXML2.DomDocument

+0

我加了這一切,但得到了同樣的錯誤。看上面的更新。 – johnny

+0

不確定。我試過這個:'xmldoc。loadXML的(」 加託說話 hablar big grande「)'並且XML加載正確,所以XML本身就OK – 2011-08-30 15:43:26