通过 VB6 读取多个 XML 文件 [英] Reading multiple XML Files via VB6
问题描述
我必须将多个巨大的 xml 数据文件导入 Excel.我不能使用简单的 loadXML()
函数,因为 Excel 没有足够的可用 RAM.(一些 xml 文件约为 100mb)
I gotta import multiple huge xml data files into Excel. I cannot use the simple loadXML()
function since Excel doesn't have enough RAM available. (some of the xml files are ~100mb)
现在我真的尝试了很多......但根本无法真正实现它.示例 XML 文件:
Now I've really tried out a lot... But couldn't really make it happen at all. Example XML File:
<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 中的内容是这样的:
So what I have in VBA is something like that:
Sub ParseXmlDocument()
Dim doc As New MSXML2.DOMDocument
Dim success As Boolean
success = doc.Load(App.Path & " est.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
总是显示为空,我无法实现.
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.
希望有人可以帮助我.
感谢 GSerg,我能够解决问题.解决方案
Thanks to GSerg i was able to solve the problem. 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
推荐答案
您的源 XML 包含命名空间,但您的 xPath 查询不包含.因此,xPath 将寻找具有空命名空间的节点,而您没有任何命名空间.
Your source XML contains namespaces, but your xPath query does not. So the xPath will be looking for nodes with empty namespace, and you don't have any.
为了修复它,您需要在 xPath 查询中提供命名空间.执行此操作的方法因使用的 XML 库而异.对于 MSXML,您需要在 DOMDocument
设置 SelectionNamespaces
属性> 对象以包含您的命名空间和前缀:
In order to fix it you need to provide a namespace in your xPath query. Ways to do that differ based on the XML library used. For MSXML, you need to set the SelectionNamespaces
property on the DOMDocument
object to include your namespace with a prefix:
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")
这篇关于通过 VB6 读取多个 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!