在vb6中读取xml文件 [英] reading xml files in vb6
问题描述
我知道在 vb.net 中读取 xml 文件更容易,但由于我们的应用程序仍在 vb6 上,我需要解决.但不知何故,我被卡住了.我也无法控制 xml 文件,因为它是从另一个应用程序生成的.来自 xml 文件的短代码如下,
I know it is easier to read xml files in vb.net but since our appl is still on vb6, i need a work around. but somehow, i am stuck. also i do not have control over the xml file as it is being generated from another application. Short code from the xml file is below,
<Report>
<Categories>
<Category name="CASHMAN" value="Cash Management" />
<Category name="IM" value="Inventory Management" />
<Category name="POS" value="Point of Sale" />
<Category name="PRODUCT" value="Product" />
</Categories>
</Report>
如果 XML 文件采用这种格式,我就可以轻松阅读.
If the XML file would have been in a format like this, i would have been able to read it easily.
<Report>
<Categories>
<name>CASHMAN</name>
<value>Cash Management</value>
</Categories>
<Categories>
<name>IM</name>
<value>Inventory Management</value>
</Categories>
<Categories>
<name>POS</name>
<value>Point of Sale</value>
</Categories>
<Categories>
<name>PRODUCT</name>
<value>Product</value>
<Categories>
<Report>
但是由于生成的 xml 文件不在我的控制范围内,所以我已经坚持了几个小时.
But since the xml file generated is not in my control, i m stuck up this since couple of hours now.
我需要从这个 xml 文件中读取 NAME-VALUE 对.我该怎么做?
i need to read the NAME-VALUE pairs from this xml file. how do i go about with this?
请帮忙.
推荐答案
您可以使用 MSXML,它提供与某些 .NET XML API 类似的功能.我现在没有 VB6 的副本,但这很容易.首先,从您的 VB6 项目中添加对 MSXML 的引用.然后,您将执行以下操作:
You can do it with MSXML, which offers similar functionality as some of the .NET XML APIs. I don't have a copy of VB6 right now, but it's pretty easy. First, add a reference to MSXML from you VB6 project. You would then do something like the following:
- 创建 MSXML2.DOMDocument的实例一个>
- 调用 Load 方法以解析 XML 文件
- 调用
selectNodes("/Report/Categories/Category")
.这将返回一个 IXMLDOMNodeList 对象. - 然后您可以遍历节点列表,检索每个 IXMLDOMNode 通过item或 nextNode.
- 然后您可以使用
attributes
属性的 XMLDOMNode 或使用selectSingleNode("@name").Text
和selectSingleNode("@value").Text
- Create an instance of MSXML2.DOMDocument
- Call the Load method to parse the XML file
- Call the
selectNodes("/Report/Categories/Category")
. This will return an IXMLDOMNodeList object. - You can then loop through the node list retrieving each IXMLDOMNode via item or nextNode.
- You can then get the
name
andvalue
using theattributes
property of the XMLDOMNode or usingselectSingleNode("@name").Text
andselectSingleNode("@value").Text
MSXML 相当灵活,因此您可以使用更短的语法,但以上应该适合您.如果你还没有弄清楚,我会在安装了 VB6 的机器上发布代码.
MSXML is fairly flexible, so there is even shorter syntax that you can use, but the above should work out for you. If you have not already figured it out, I will post the code when I get to a machine with VB6 installed.
UDPATE:
这是一个使用您提供的 XML 示例的工作示例.
Here is a working example using the XML sample you provided.
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("/Report/Categories/Category")
If Not nodeList Is Nothing Then
Dim node As MSXML2.IXMLDOMNode
Dim name As String
Dim value As String
For Each node In nodeList
' Could also do node.attributes.getNamedItem("name").text
name = node.selectSingleNode("@name").Text
value = node.selectSingleNode("@value").Text
Next node
End If
End If
End Sub
这篇关于在vb6中读取xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!