xml 在vb6中读取xml文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3870485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:19:38  来源:igfitidea点击:

reading xml files in vb6

xmlvb6

提问by Ubaid

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,

我知道在 vb.net 中读取 xml 文件更容易,但由于我们的应用程序仍在 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>

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, i m stuck up this since couple of hours now.

但是由于生成的 xml 文件不在我的控制范围内,所以我已经坚持了几个小时。

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

我需要从这个 xml 文件中读取 NAME-VALUE 对。我该怎么办?

please help.

请帮忙。

回答by Garett

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 MSXMLfrom you VB6 project. You would then do something like the following:

您可以使用MSXML 来实现,它提供与某些 .NET XML API 类似的功能。我现在没有 VB6 的副本,但这很容易。首先,从您的 VB6 项目中添加对MSXML的引用。然后,您将执行以下操作:

  • Create an instance of MSXML2.DOMDocument
  • Call the Loadmethod to parse the XML file
  • Call the selectNodes("/Report/Categories/Category"). This will return an IXMLDOMNodeListobject.
  • You can then loop through the node list retrieving each IXMLDOMNodevia itemor nextNode.
  • You can then get the nameand valueusing the attributesproperty of the XMLDOMNode or using selectSingleNode("@name").Textand selectSingleNode("@value").Text
  • 创建MSXML2.DOMDocument的实例
  • 调用Load方法解析 XML 文件
  • 调用selectNodes("/Report/Categories/Category"). 这将返回一个IXMLDOMNodeList对象。
  • 然后,您可以遍历节点列表,通过itemnextNode检索每个IXMLDOMNode
  • 然后,您可以获取namevalue使用attributesXMLDOMNode的属性或使用selectSingleNode("@name").TextselectSingleNode("@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.

MSXML 相当灵活,因此您可以使用更短的语法,但以上应该适合您。如果您还没有弄清楚,我会在安装了 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

回答by MarkJ

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

按照此问题(以及 Ardman 链接的文章)中的建议使用MSXML

You can use IXMLDOMElement.getAttributeNodeto read attributes.

您可以使用IXMLDOMElement.getAttributeNode来读取属性。

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

例如,下面的代码从 MSDN读取示例 books.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

回答by Iain

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

回答by Lovsan

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