【问题标题】:getElementsByTagName returning zero elements with DOMDOCUMENT60 in VBAgetElementsByTagName 在 VBA 中使用 DOMDOCUMENT60 返回零元素
【发布时间】:2020-07-24 12:58:58
【问题描述】:

我有一段 VBA 代码可以在旧环境中完美运行,使用 MSXML2.DOMDOCUMENT 对象。但是,现在我不得不将这些转换为 MSXML2.DOMDOCUMENT60 对象(因为 Excel 64 位不支持 MSXML2.DOMDOCUMENT)它不再起作用。

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

请注意,数据都存在于文件中,如果我在 VBA 编辑器中使用 Locals 窗口,那么我可以检查所有数据。它只是拒绝被代码返回。

这是我正在使用的代码:

' 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 文件的 sn-p:

<?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>

那我做错了什么?谢谢。

【问题讨论】:

  • getElementsByTagNamedocumented 以忽略命名空间并匹配本地名称。我不确定什么设置会破坏这种行为,但正如同一篇文章中所建议的,您可以改用.selectNodes("//t:VehicleJourney")。请注意,您在 XML 和代码中有不同拼写的命名空间(末尾缺少斜杠),您必须执行 .setProperty "SelectionNamespaces", "xmlns:t=""http://www.transxchange.org.uk/""" 并在查询中使用前缀。
  • 那太可怕了 - getElementsByTagName 根本上被破坏了,(如建议的那样)我正在使用 TXCDoc.selectNodes("//txc:VehicleJourneys/txc:VehicleJourney") 代替。鉴于 XML 可以有一个默认的命名空间,它宁愿相信微软不能产生一个实际让你使用它的例程!现在我所有的路径都将真的冗长。

标签: xml vba msxml


【解决方案1】:

感谢@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2011-05-26
    • 2021-10-13
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多