通过VB6读取多个XML文件

时间:2014-09-25 08:55:16

标签: xml excel-vba vb6 vba excel

我必须将多个巨大的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

1 个答案:

答案 0 :(得分:1)

您的源XML包含名称空间,但您的xPath查询却没有。因此,xPath将查找具有空命名空间的节点,并且您没有任何。

为了解决这个问题,您需要在xPath查询中提供命名空间。如何做到这一点differ based on the XML library used。对于MSXML,您需要在DOMDocument对象上set the SelectionNamespaces property以包含带有前缀的命名空间:

doc.setProperty("SelectionNamespaces", "xmlns:t='urn:omds20'")

然后更改您的查询以使用该前缀:

Set nodeList = doc.selectNodes("/t:OMDS/t:PAKET/t:PROVISION")