[英]Reading multiple XML Files via VB6
我必須將多個巨大的xml數據文件導入Excel。 我不能使用簡單的loadXML()
函數,因為Excel沒有足夠的可用RAM。 (某些xml文件約為100mb)
現在我真的已經嘗試了很多……但是根本無法實現。 示例XML文件:
<OMDS xmlns="urn:omds20" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:omds20 ./omds24-00.xsd">
<PAKET VUNr="1" MaklerID="2" PaketZpktErstell="x" PaketZpktLetztErstell="y">
<PROVISION ProvisionsID="123" Polizzennr="321" Vermnr="5" BuchDat="2013-02-27"/>
<PROVISION ProvisionsID="456" Polizzennr="321" Vermnr="5" BuchDat="2013-02-27"/>
<PROVISION ProvisionsID="789" Polizzennr="321" Vermnr="5" BuchDat="2013-02-27"/>
</PAKET>
</OMDS>
所以我在VBA中擁有這樣的東西:
Sub ParseXmlDocument()
Dim doc As New MSXML2.DOMDocument
Dim success As Boolean
success = doc.Load(App.Path & "\test.xml")
If success = False Then
MsgBox doc.parseError.reason
Else
Dim nodeList As MSXML2.IXMLDOMNodeList
Set nodeList = doc.selectNodes("/OMDS/PAKET/PROVISION")
If Not nodeList Is Nothing Then
Dim node As MSXML2.IXMLDOMNode
Dim idAs String
Dim value As String
For Each node In nodeList
id= node.selectSingleNode("ProvisionsID").Text
Next node
End If
End If
End Sub
之后,我只是嘗試在MsgBox
打印ID,但由於nodeList
始終顯示為空,因此無法實現。
希望有人可以幫助我。
多虧了GSerg,我才得以解決問題。 解決方案
Sub ParseXmlDocument()
Dim doc As New MSXML2.DOMDocument
Dim success As Boolean
With doc
.async = False
.setProperty "SelectionLanguage", "XPath"
.setProperty "SelectionNamespaces", "xmlns:t='urn:omds20'"
End With
success = doc.Load("C:\...\demo.xml")
If success = False Then
MsgBox doc.parseError.reason
Else
Dim nodeList As MSXML2.IXMLDOMNodeList
Set nodeList = doc.SelectNodes("/t:OMDS/t:PAKET/t:PROVISION")
If Not nodeList Is Nothing Then
Dim node As MSXML2.IXMLDOMNode
Dim id As String
Dim value As String
For Each node In nodeList
id = node.SelectSingleNode("@ProvisionsID").Text
Next node
End If
End If
End Sub
您的源XML包含名稱空間,但您的xPath查詢不包含名稱空間。 因此,xPath將尋找名稱空間為空的節點,而您沒有任何節點。
為了修復它,您需要在xPath查詢中提供一個名稱空間。 實現方法因所使用的XML庫而異 。 對於MSXML,您需要在DOMDocument
對象上設置SelectionNamespaces
屬性以包含帶有前綴的名稱空間:
doc.setProperty("SelectionNamespaces", "xmlns:t='urn:omds20'")
然后更改您的查詢以使用該前綴:
Set nodeList = doc.selectNodes("/t:OMDS/t:PAKET/t:PROVISION")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.