从 XML 中提取值失败
Extracting value from XML fails
我搜索了许多关于阅读 XML 文件的示例和教程,但我无法从单个 XML 文档中提取价值。我想我对 Value、SingleNode、Text 等声明感到困惑。
<?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 runtime error '800a01a8'
Object required
假设您实际上是 运行 来自 ASP 页面的代码,我怀疑您在加载文件时遇到问题(可能不在网络服务器的当前工作目录中过程)。由于 Load
和 SelectSingleNode
都会静默失败而不会引发错误,因此您需要在加载文件后检查 ParseError
属性 的值:
xmlDOM.Load("file.xml")
If xmlDOM.ParseError <> 0 Then
response.write xmlDOM.ParseError.Reason
Else
response.write "file loaded"
End If
我搜索了许多关于阅读 XML 文件的示例和教程,但我无法从单个 XML 文档中提取价值。我想我对 Value、SingleNode、Text 等声明感到困惑。
<?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 runtime error '800a01a8'
Object required
假设您实际上是 运行 来自 ASP 页面的代码,我怀疑您在加载文件时遇到问题(可能不在网络服务器的当前工作目录中过程)。由于 Load
和 SelectSingleNode
都会静默失败而不会引发错误,因此您需要在加载文件后检查 ParseError
属性 的值:
xmlDOM.Load("file.xml")
If xmlDOM.ParseError <> 0 Then
response.write xmlDOM.ParseError.Reason
Else
response.write "file loaded"
End If