我有一段 VBA 代码,可以在旧环境中完美运行,使用 MSXML2.DOMDOCUMENT
对象。然而,现在我不得不把它们变成 MSXML2.DOMDOCUMENT60
对象(因为 Excel 64 位不支持 MSXML2.DOMDOCUMENT
)它不再起作用。
具体来说,我得到 getElementsByTagName
返回零个节点(尽管我也遇到类似的问题,例如 selectNodes
或 selectSingleNode
。我怀疑命名空间(并且我已经阅读了无数关于该主题的不同帖子),但我无法使其工作。
请注意,数据全部存在于文件中,如果我使用 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/