我有一个来自 Web 服务的 XML 文件。我正在尝试使用 VBA 中的 Microsoft XML v6 对象来解析它。我的代码是:
Public Sub LoadDocument()
Dim xDoc As MSXML2.DOMDocument60
Set xDoc = New MSXML2.DOMDocument60
resp = xDoc.Load("c:\xmlsoccer - scot prem.txt")
Debug.Print xDoc.parseError.errorCode
Debug.Print xDoc.parseError.reason
Debug.Print xDoc.parseError.Line
End Sub
resp 返回 false,错误原因是“在文档的顶层无效”。
XML 字符串的开头是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<XMLSOCCER.COM>
<Match>
<Id>70146</Id>
<FixtureMatch_Id>332875</FixtureMatch_Id>
<Date>2014-05-11T11:15:00+00:00</Date>
<Round>38</Round>
<Spectators>17011</Spectators>
<League>Scottish Premier League</League>
<HomeTeam>Aberdeen</HomeTeam>
<HomeTeam_Id>45</HomeTeam_Id>
....
我尝试删除第一行但得到同样的错误。文本格式是否正确?对 XML 不太了解,所以甚至不确定我是否可以将其加载到 DOM 对象中。
最佳答案
确保将 Microsoft XML、V6.0 的引用添加到 VBA 模块。我使用了以下 XML,您的代码没有问题:
<?xml version="1.0" encoding="UTF-8"?>
<XMLSOCCER.COM>
<Match>
<Id>70146</Id>
<FixtureMatch_Id>332875</FixtureMatch_Id>
<Date>2014-05-11T11:15:00+00:00</Date>
<Round>38</Round>
<Spectators>17011</Spectators>
<League>Scottish Premier League</League>
<HomeTeam>Aberdeen</HomeTeam>
<HomeTeam_Id>45</HomeTeam_Id>
</Match>
</XMLSOCCER.COM>
关于xml - 在 VBA 中使用 DOM 解析 XML 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919886/