2014-09-25 249 views
0

我要将多个巨大的xml数据文件导入Excel。我无法使用简单的loadXML()函数,因为Excel没有足够的RAM可用。 (一些xml文件是〜100MB)通过VB6读取多个XML文件

现在我真的尝试了很多...但是不能真的让它发生。 示例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 
+0

你的节点属于一个命名空间,但是您不在xpath查询中提供任何名称空间。另外'Load'是异步的,除非使用'.async = False',所以你在加载完成之前检查是否成功。 – GSerg 2014-09-25 09:04:37

+0

如何为xpath查询提供名称空间?第一次使用XML sry: - /感谢提示与异步:) – cosi 2014-09-25 09:10:00

+0

http://stackoverflow.com/q/2141181/11683 – GSerg 2014-09-25 09:33:08

回答

1

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

为了解决它,您需要在xPath查询中提供一个名称空间。方法做到这一点differ based on the XML library used。对于MSXML,你需要set the SelectionNamespaces propertyDOMDocument对象为包括前缀命名空间:

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

,然后更改查询中使用前缀:

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

非常感谢你...真的有它的工作......谢谢!英雄! – cosi 2014-09-25 10:47:37