简体   繁体   English

通过VB6读取多个XML文件

[英]Reading multiple XML Files via VB6

I gotta import multiple huge xml data files into Excel. 我必须将多个巨大的xml数据文件导入Excel。 I cannot use the simple loadXML() function since Excel doesn't have enough RAM available. 我不能使用简单的loadXML()函数,因为Excel没有足够的可用RAM。 (some of the xml files are ~100mb) (某些xml文件约为100mb)

Now I've really tried out a lot... But couldn't really make it happen at all. 现在我真的已经尝试了很多……但是根本无法实现。 Example XML File: 示例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>

So what I have in VBA is something like that: 所以我在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

After that I'm just trying to Print the ID within a MsgBox , but since the nodeList always appears to be empty, I can't make it happen. 之后,我只是尝试在MsgBox打印ID,但由于nodeList始终显示为空,因此无法实现。

Hope someone can help me out. 希望有人可以帮助我。


Thanks to GSerg i was able to solve the problem. 多亏了GSerg,我才得以解决问题。 Here the Solution 解决方案

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

Your source XML contains namespaces, but your xPath query does not. 您的源XML包含名称空间,但您的xPath查询不包含名称空间。 So the xPath will be looking for nodes with empty namespace, and you don't have any. 因此,xPath将寻找名称空间为空的节点,而您没有任何节点。

In order to fix it you need to provide a namespace in your xPath query. 为了修复它,您需要在xPath查询中提供一个名称空间。 Ways to do that differ based on the XML library used . 实现方法因所使用的XML库 For MSXML, you need to set the SelectionNamespaces property on the DOMDocument object to include your namespace with a prefix: 对于MSXML,您需要在DOMDocument对象上设置SelectionNamespaces属性以包含带有前缀的名称空间:

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

And then change your query to use that prefix: 然后更改您的查询以使用该前缀:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM