xml - getElementsByTagName 在 VBA 中使用 DOMDOCUMENT60 返回零个元素

标签 xml vba msxml

我有一段 VBA 代码,可以在旧环境中完美运行,使用 MSXML2.DOMDOCUMENT对象。然而,现在我不得不把它们变成 MSXML2.DOMDOCUMENT60对象(因为 Excel 64 位不支持 MSXML2.DOMDOCUMENT )它不再起作用。

具体来说,我得到 getElementsByTagName 返回零个节点(尽管我也遇到类似的问题,例如 selectNodesselectSingleNode 。我怀疑命名空间(并且我已经阅读了无数关于该主题的不同帖子),但我无法使其工作。

请注意,数据全部存在于文件中,如果我使用 VBA 编辑器中的“本地”窗口,那么我可以检查所有数据。它只是拒绝由代码返回。

这是我正在使用的代码:

' Open the TXC XML file
Dim TXCDoc As New msxml2.DOMDocument60
With TXCDoc
    .async = False
    .validateOnParse = True
    .setProperty "SelectionLanguage", "XPath"
    .setProperty "SelectionNamespaces", "xmlns=""http://www.transxchange.org.uk"""
    .Load filename
End With

If TXCDoc.parseError.ErrorCode <> 0 Then
    MsgBox "Parsing error in file " & filename & Chr$(10) & Chr$(10) & TXCDoc.parseError.reason & Chr$(10) & Chr$(10) & _
           "Line: " & TXCDoc.parseError.Line & ":" & TXCDoc.parseError.linepos, vbCritical + vbOKOnly, "TXC Validation Error"
End If
    
' Read the VehicleJourneys    
Dim xmlVjList As IXMLDOMNodeList
Dim xmlVj As IXMLDOMNode
Set xmlVjList = TXCDoc.getElementsByTagName("VehicleJourney")
For Each xmlVj In xmlVjList
    ' Do stuff
Next xmlVj

[编辑澄清 - xmVjList返回零 VehicleJourney节点]

这是我尝试加载的 XML 文件的片段:

<?xml version="1.0" encoding="utf-8"?>
<TransXChange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.transxchange.org.uk/ http://www.transxchange.org.uk/schema/2.1/TransXChange_general.xsd" CreationDateTime="2020-07-21T09:57:00-00:00" ModificationDateTime="2020-07-21T09:57:00-00:00" Modification="new" RevisionNumber="0" FileName="SVRYEAG001.xml" SchemaVersion="2.1" RegistrationDocument="false" xmlns="http://www.transxchange.org.uk/">
  <StopPoints> ... </StopPoints>
  <RouteSections> ... </RouteSections>
  <Routes> ... </Routes>
  <JourneyPatternSections> ... </JourneyPatternSections>
  <Operators> ... </Operators>
  <Services> ... </Services>
  <VehicleJourneys>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <VehicleJourney> ... </VehicleJourney>
    <!-- there are 22 VehicleJourney nodes in total -->
  </VehicleJourneys>
</TransXChange>

那我做错了什么?谢谢。

最佳答案

感谢@gserg,这是我现在使用的代码:

' Open the TXC XML file
Dim TXCDoc As New msxml2.DOMDocument60
With TXCDoc
    .async = False
    .validateOnParse = True
    .setProperty "SelectionLanguage", "XPath"
    .setProperty "SelectionNamespaces", "xmlns:txc=""http://www.transxchange.org.uk/"""
    .Load filename
End With

If TXCDoc.parseError.ErrorCode <> 0 Then
    MsgBox "Parsing error in file " & filename & Chr$(10) & Chr$(10) & TXCDoc.parseError.reason & Chr$(10) & Chr$(10) & _
       "Line: " & TXCDoc.parseError.Line & ":" & TXCDoc.parseError.linepos, vbCritical + vbOKOnly, "TXC Validation Error"
End If
    
' Read the VehicleJourneys    
Dim xmlVjList As IXMLDOMNodeList
Dim xmlVj As IXMLDOMNode
Set xmlVjList = TXCDoc.selectNodes("//txc:VehicleJourneys/txc:VehicleJourney")
For Each xmlVj In xmlVjList
    ' Do stuff
Next xmlVj

关于xml - getElementsByTagName 在 VBA 中使用 DOMDOCUMENT60 返回零个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63074067/

相关文章:

c++ - 有没有办法修改样式表,以便将带有空标签的 XML 文档转换为 <tag/>?

delphi - MSXML - 命名空间信息不持久

android - 去除 CardView 的圆角

c# - 使用 C# 解析 XML

java - Mybatis xml resultMap 存在集合和关联问题

MySQL 记录集在应返回值时未返回值

vba - 应该为 .Find(LookAt : = xlPart/xlWhole)? 选择哪种变量类型

java - 将图表存储在数据库中

excel - 如何在 Excel VBA 中将一组值分配给一个范围

vb6 - 如何使用 VB6 和 MSXML 漂亮地打印 XML 源代码?