2016-05-19 42 views
0

我已經搜索了許多有關讀取XML文件的示例和教程,但我無法從單個XML文檔中提取值。我覺得我對Value,SingleNode,Text等聲明感到困惑。從XML中提取值失敗

<?xml version="1.0" encoding="utf-8"?> 
<resultObj> 
    <result>False</result> 
    <invoiceNumber>1</invoiceNumber> 
    <invoiceDate>2016/05/18 08:26:35</invoiceDate> 
</resultObj> 

的VBScript(經典ASP)來讀取結果:

Set xmlDOM = Server.CreateObject("MSXML2.DOMDocument") 
xmlDOM.async = False 
xmlDOM.setProperty "ServerHTTPRequest", True 
xmlDOM.Load("file.xml") 
Set myroot= xmlDOM.selectSingleNode("/resultObj/result") 
response.write myroot.Text 

錯誤在最後一行:

Microsoft VBScript運行時錯誤 '800a01a8'
所需的對象

回答

3

假設你實際運行從ASP頁面編寫這段代碼我懷疑在加載文件時可能有問題(可能不在Web服務器進程的當前工作目錄中)。由於兩個LoadSelectSingleNode將靜默失敗而不產生錯誤,你需要加載該文件後檢查ParseError屬性的值:

xmlDOM.Load("file.xml") 
If xmlDOM.ParseError <> 0 Then 
    response.write xmlDOM.ParseError.Reason 
Else 
    response.write "file loaded" 
End If 
+0

謝謝你這麼多。我發佈了一個與此相關的新問題。請檢查是否可以幫助.http://stackoverflow.com/questions/37339797/loading-xml-file-from-httprequest-output –