VBA-什么是获取标记名称的XPath语法 [英] VBA - What's The XPath Syntax To Get Tag Names

查看:112
本文介绍了VBA-什么是获取标记名称的XPath语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA宏来解析XML文件.给出以下结构:

I am trying to use a VBA macro to parse XML file. Given with the following structure:

  <bookstore>
  <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
  </bookstore>

如何枚举带有元素标签及其对应值的输出,如下所示?

How can I enumerate the output with element tags with its corresponding values as shown below?

book | category | children
title | harry potter
author | J K. Rowling
...

我的代码如下:

Set xmlFile = CreateObject("Microsoft.XMLDOM")
xmlFile.Load (file)
Set qXML = xmlFile.SelectNodes("/bookstore")
For i = 0 To qXML.Length - 1
  Debug.Print CStr(qXML(i).Text)
Next i

推荐答案

如何获取标签名称

"获取标记名的XPath语法是什么?"

严格来说,获取 .Name 和/或 .NodeName 属性是(XML)DOM 语法; XMLDOM(文档对象模型)是一个跨平台且与语言无关的界面,将文档视为树结构并允许通过编程方式访问树.

Strictly speaking it's (XML)DOM syntax to to get .Name and/or .NodeName properties; XMLDOM (Document Object Model) is a cross-platform and language-independent interface treating the document as a tree structure and allowing programmatic access to the tree.

您可以使用 XPath 表达式的特殊语法(例如"/bookstore/book/title" )来解决分层xml中的任何逻辑部分文档结构.

You can use, however the special syntax of XPath expressions (like e.g. "/bookstore/book/title") to address any logical part in the hierarchical xml document structure.

因此,与您的OP接近的解决方案是:

So a solution close to your OP would be:

Option Explicit             ' declaration head of your code module

Sub ExampleCall()
    Dim file As String: file = ThisWorkbook.Path & "\xml\bookstore.xml"
    Dim xmlFile As Object
    Set xmlFile = CreateObject("Microsoft.XMLDOM")
    If xmlFile.Load(file) Then
        Dim qXML As Object
        Set qXML = xmlFile.DocumentElement.SelectNodes("book")
        Dim q As Object
        For Each q In qXML
            Dim cnt As Long: cnt = cnt + 1
            Debug.Print Format(cnt, "--- 000 ---")
            Debug.Print q.Attributes(0).Name, "|" & q.Attributes(0).Text
            Dim i As Long
            For i = 0 To q.ChildNodes.Length - 1
                Debug.Print q.ChildNodes(i).nodeName, "|" & q.ChildNodes(i).Text
            Next
        Next
    End If
End Sub

在VBE的即时窗口中显示结果

--- 01 ---
category      |children
title         |Harry Potter
author        |J K. Rowling
year          |2005
price         |29.99
--- 02 ---
category      |web
title         |Learning XML
author        |Erik T. Ray
year          |2003
price         |39.95

旁注

由于已弃用 Microsoft.XMLDOM 多年,我希望在最新的xml版本 Microsoft XML,v6.0 中绑定到► MSXML2 ,例如通过

As Microsoft.XMLDOM has been deprecated for years, I'd prefer binding to ►MSXML2 in the most current xml version Microsoft XML,v6.0, e.g. via

I.LATE绑定(如OP中一样)

    Dim xDoc As Object
    Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")

II.早期绑定

    Dim xDoc As MSXML2.DOMDocument60     ' *) whereas MSXML2.DOMDocument (=old version 3.0)
    Set xDoc = New MSXML2.DOMDocument60  ' mind the missing point in digits

旁注:OP使用对象变量 XMLFile 而不是 xDoc

Side note: OP uses the object variable XMLFile instead of xDoc

注意,默认情况下,引用 DOMDocument 而不进行明显版本控制的对象将在内部绑定到3.0(6.0之前的最后一个稳定版本,不建议使用其他任何版本).

Note that referencing DOMDocument without obvious versioning would bind internally to 3.0 by default (the last stable version before 6.0, any other versions are deprecated).

其他链接

XML解析...

这篇关于VBA-什么是获取标记名称的XPath语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆