|
|
|
|
|
本文介紹VB6.0是如何讀取XML文件的。
本示例VB用“Microsoft.XMLHTTP
”創(chuàng)建對(duì)象來讀取XML文件。
因此須首先在菜單“工程”里引用“Microsoft XML 3.0”
XML文件中的簡短代碼如下:
<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內(nèi)容。
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
name = node.Attributes.getNamedItem("name ").Text
value = node.Attributes.getNamedItem("value").Text
Next node
End If
End If
End Sub
通過本文介紹的方法,可以輕松獲取本文示例格式的XML內(nèi)容。