如何在VB6中读取XML文件?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (854)

我知道在vb.net中读取XML文件比较容易,但是由于我们的APPL仍然在VB6上,所以我需要一个工作。但是不知怎么的,我很紧张。而且,我无法控制XML文件,因为它是从另一个应用程序生成的。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文件是这样的格式,我就可以轻松地读取它。

    <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文件不在我的控制范围内,所以我已经坚持了几个小时。

我需要从这个xml文件中读取NAME-VALUE对。

提问于
用户回答回答于
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
用户回答回答于
Private Sub Form_Load()
Dim xmlDoc As New MSXML2.DOMDocument30
Dim nodeBook As IXMLDOMElement
Dim nodeId As IXMLDOMAttribute
Dim sIdValue As String
xmlDoc.async = False
xmlDoc.Load App.Path & "\books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox ("You have error " & myErr.reason)
Else
   Set nodeBook = xmlDoc.selectSingleNode("//book")
   Set nodeId = nodeBook.getAttributeNode("id")
   sIdValue = nodeId.xml
   MsgBox sIdValue
End If

End Sub

扫码关注云+社区

领取腾讯云代金券