reading xml files in vb6

前端 未结 4 1414
时光取名叫无心
时光取名叫无心 2020-12-02 23:35

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 fi

相关标签:
4条回答
  • 2020-12-03 00:18

    Use MSXML as advised in this question (and in the article linked by Ardman).

    You can use IXMLDOMElement.getAttributeNode to read attributes.

    For example this code below reads the sample books.xml file from MSDN and accesses an attribute. You need a reference to a version of Microsoft XML.

    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
    
    0 讨论(0)
  • 2020-12-03 00:30

    you could uses XSLT to transform the XML from this structure to value pair

    http://www.xmlfiles.com/articles/sample_chapters/sams_xmlforaspnet/default.asp

    0 讨论(0)
  • 2020-12-03 00:37

    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 & "\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
    
    0 讨论(0)
  • 2020-12-03 00:38

    Thank you, this questions answers helped me a lot. Took me 2 days to figure how,

    Set xmlDoc = CreateObject("Msxml2.DOMDocument")
    
    Dim nodeBook
    Dim nodeId
    xmlDoc.async = False
    xmlDoc.Load ("xmlfile url")
    If (xmlDoc.parseError.errorCode <> 0) Then
       Dim myErr
       Set myErr = xmlDoc.parseError
       MsgBox ("You have error " & myErr.reason)
    Else
       Set nodeBook = xmlDoc.selectSingleNode("//Program")
       Set nodeId = nodeBook.getAttributeNode("description")
       wscript.Echo nodeId.value
    End If
    
    0 讨论(0)
提交回复
热议问题