简体   繁体   English

在vb6中读取xml文件

[英]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.我知道在 vb.net 中读取 xml 文件更容易,但由于我们的应用程序仍在 vb6 上,我需要解决。 but somehow, i am stuck.但不知何故,我被卡住了。 also i do not have control over the xml file as it is being generated from another application.我也无法控制 xml 文件,因为它是从另一个应用程序生成的。 Short code from the xml file is below,来自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>

If the XML file would have been in a format like this, i would have been able to read it easily.如果 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>

But since the xml file generated is not in my control, im stuck up this since couple of hours now.但是由于生成的 xml 文件不在我的控制范围内,所以我已经坚持了几个小时。

i need to read the NAME-VALUE pairs from this xml file.我需要从这个 xml 文件中读取 NAME-VALUE 对。 how do i go about with this?我该怎么办?

please help.请帮忙。

You can do it with MSXML , which offers similar functionality as some of the .NET XML APIs.您可以使用MSXML 来实现,它提供与某些 .NET XML API 类似的功能。 I don't have a copy of VB6 right now, but it's pretty easy.我现在没有 VB6 的副本,但这很容易。 First, add a reference to MSXML from you VB6 project.首先,从您的 VB6 项目中添加对MSXML的引用。 You would then do something like the following:然后,您将执行以下操作:

  • Create an instance of MSXML2.DOMDocument创建MSXML2.DOMDocument的实例
  • Call the Load method to parse the XML file调用Load方法解析 XML 文件
  • Call the selectNodes("/Report/Categories/Category") .调用selectNodes("/Report/Categories/Category") This will return an IXMLDOMNodeList object.这将返回一个IXMLDOMNodeList对象。
  • You can then loop through the node list retrieving each IXMLDOMNode via item or nextNode .然后,您可以遍历节点列表,通过itemnextNode检索每个IXMLDOMNode
  • You can then get the name and value using the attributes property of the XMLDOMNode or using selectSingleNode("@name").Text and selectSingleNode("@value").Text然后,您可以使用 XMLDOMNode 的attributes属性或使用selectSingleNode("@name").TextselectSingleNode("@value").Text获取namevalue

MSXML is fairly flexible, so there is even shorter syntax that you can use, but the above should work out for you. MSXML 相当灵活,因此您可以使用更短的语法,但以上应该适合您。 If you have not already figured it out, I will post the code when I get to a machine with VB6 installed.如果您还没有弄清楚,我会在安装了 VB6 的机器上发布代码。

UDPATE: UDPATE:

Here is a working example using the XML sample you provided.这是一个使用您提供的 XML 示例的工作示例。

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

Use MSXML as advised in this question (and in the article linked by Ardman).按照此问题(以及 Ardman 链接的文章)中的建议使用MSXML

You can use IXMLDOMElement.getAttributeNode to read attributes.您可以使用IXMLDOMElement.getAttributeNode来读取属性。

For example this code below reads the sample books.xml file from MSDN and accesses an attribute.例如,下面的代码从 MSDN 读取 示例 books.xml 文件并访问一个属性。 You need a reference to a version of Microsoft XML .您需要对 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

you could uses XSLT to transform the XML from this structure to value pair您可以使用 XSLT 将 XML 从此结构转换为值对

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

Thank you, this questions answers helped me a lot.谢谢,这些问题的答案对我帮助很大。 Took me 2 days to figure how,我花了 2 天的时间才弄清楚如何,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM