Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

reading xml files in vb6

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>

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>

But since the xml file generated is not in my control, i m stuck up this since couple of hours now.

i need to read the NAME-VALUE pairs from this xml file. how do i go about with this?

please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • 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 and value using the attributes property of the XMLDOMNode or using selectSingleNode("@name").Text and selectSingleNode("@value").Text

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:

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 & "est.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...