VBA DOM XPath 属性中的变量

VBA DOM Variable in XPath Attribute

我有一个程序可以搜索和收集(通过数组)特定作者及其 'BookType' 和 'BookTitle' 我现在正在尝试学习如何使用作者的名字 - 我'已存储在数组中 - 作为 XPath 中的变量以获取 'Store Location'。类似于:“/catalog/book/misc/PublisherAuthor id="myvar"/StoreLocation”

三件事让我很困惑: 1. 如果变量是数组(如果可能的话),您将如何在 XPath 中声明它? 2. 在 For 循环中声明这样的语句是不是一个坏主意? 3.我的XPath/DOM逻辑正确吗?

<?xml version="1.0"?>
<catalog>
<book id="Adventure">
   <author>Gambardella, Matthew</author>
   <title>XML Developer's Guide</title>
   <price>44.95</price>
   <misc>
        <Publisher id="5691">
            <PublisherLocation>Los Angeles</PublisherLocation>
        </Publisher>
        <PublishedAuthor id="Gambardella, Matthew">
            <StoreLocation>Store B</StoreLocation>
        </PublishedAuthor>
    </misc>
</book>
<book id="Adventure">
   <author>Ralls, Kim</author>
   <title>Midnight Rain</title>
   <price>5.95</price>
   <misc>
        <Publisher id="4787">
            <PublisherLocation>New York</PublisherLocation>
        </Publisher>
        <PublishedAuthor id="Ralls, Kim">
            <StoreLocation>Store B</StoreLocation>
        </PublishedAuthor>
    </misc>
</book>
<book id="Adventure">
   <author>Boal, John</author>
   <title>Mist</title>
   <price>15.95</price>
   <misc>
        <Publisher id="8101">
            <PublisherLocation>New Mexico</PublisherLocation>
        </Publisher>
        <PublishedAuthor id="Boal, John">
            <StoreLocation>Store B</StoreLocation>
        </PublishedAuthor>
    </misc>
</book>
<book id="Mystery">
   <author>Ralls, Kim</author>
   <title>Some Mystery Book</title>
   <price>9.95</price>
   <misc>
        <Publisher id="6642">
            <PublisherLocation>New York</PublisherLocation>
        </Publisher>
        <PublishedAuthor id="Ralls, Kim">
            <StoreLocation>Store B</StoreLocation>
        </PublishedAuthor>
    </misc>
</book>
</catalog>

我的代码:

Option Explicit

Sub mySub()

Dim XMLFile As Variant
Dim Author As Variant
Dim athr As String, BookType As String, Title As String, StoreLocation As String
Dim AuthorArray() As String, BookTypeArray() As String, TitleArray() As String, StoreLocationArray() As String
Dim i As Long, x As Long, j As Long

Dim mainWorkBook As Workbook
Dim n As IXMLDOMNode
Set mainWorkBook = ActiveWorkbook
Set XMLFile = CreateObject("Microsoft.XMLDOM")
XMLFile.Load ("C:\Books.xml")


x = 1
j = 0

Set Author = XMLFile.SelectNodes("/catalog/book/author")
For i = 0 To (Author.Length - 1)
    ReDim Preserve AuthorArray(0 To i)
    ReDim Preserve BookTypeArray(0 To i)
    ReDim Preserve TitleArray(0 To i)
    ReDim Preserve StoreLocationArray(0 To i)

    athr = Author(i).Text
    BookType = Author(i).ParentNode.getAttribute("id")
    Title = Author(i).ParentNode.getElementsByTagName("title").Item(0).nodeTypedValue
    StoreLocation = Author(i).ParentNode.selectSingleNode("/misc/PublishedAuthor[@id=""&athr(j)&""]/StoreLocation").NodeValue

    If athr = "Ralls, Kim" Then

        AuthorArray(j) = athr
        BookTypeArray(j) = BookType
        TitleArray(j) = Title
        StoreLocationArray(j) = StoreLocation

        j = j + 1
        x = x + 1
    End If
Next

Range("A3:A" & UBound(AuthorArray) + 1) = WorksheetFunction.Transpose(AuthorArray)
Range("B3:B" & UBound(BookTypeArray) + 1) = WorksheetFunction.Transpose(BookTypeArray)
Range("C3:C" & UBound(BookTypeArray) + 1) = WorksheetFunction.Transpose(TitleArray)
Range("D3:D" & UBound(BookTypeArray) + 1) = WorksheetFunction.Transpose(TitleArray)

具体来说,我不明白如何设置的是这一行:

StoreLocation = Author(i).ParentNode.selectSingleNode("/misc/PublishedAuthor[@id=""&athr(j)&""]/StoreLocation").NodeValue

做这样的事情不受欢迎吗?有没有更合理的方式?

感谢您提供任何指导、帮助或评论:)

您需要双引号来转义它们:

StoreLocation = Author(i).ParentNode.selectSingleNode( _
   "/misc/PublishedAuthor[@id=""" & athr & """]/StoreLocation").NodeValue

编辑:这对我有用

Sub mySub()

Dim XMLFile As Variant
Dim Author As Variant
Dim athr As String, BookType As String, Title As String, StoreLocation As String
Dim AuthorArray() As String, BookTypeArray() As String
Dim TitleArray() As String, StoreLocationArray() As String
Dim i As Long, x As Long, j As Long, locs

Dim mainWorkBook As Workbook
Dim n As IXMLDOMNode
Set mainWorkBook = ActiveWorkbook
Set XMLFile = CreateObject("Microsoft.XMLDOM")
XMLFile.Load ("C:\Books.xml")

x = 1
j = 0

Set Author = XMLFile.SelectNodes("/catalog/book/author")

For i = 0 To (Author.Length - 1)

    athr = Author(i).Text

    'moved a bunch of your code inside this check...
    If athr = "Ralls, Kim" Then

        BookType = Author(i).ParentNode.getAttribute("id")
        Title = Author(i).ParentNode.getElementsByTagName("title").Item(0).nodeTypedValue

        Set locs = Author(i).ParentNode.SelectNodes( _
               "misc/PublishedAuthor[@id='" & athr & "']/StoreLocation")

        If locs.Length > 0 Then
            StoreLocation = locs(0).nodeTypedValue
        Else
            StoreLocation = "???"
        End If

        ReDim Preserve AuthorArray(0 To j)
        ReDim Preserve BookTypeArray(0 To j)
        ReDim Preserve TitleArray(0 To j)
        ReDim Preserve StoreLocationArray(0 To j)

        AuthorArray(j) = athr
        BookTypeArray(j) = BookType
        TitleArray(j) = Title
        StoreLocationArray(j) = StoreLocation

        j = j + 1
        x = x + 1

    End If
Next

If j > 0 Then

    Range("A3").Resize(j, 1).Value = WorksheetFunction.Transpose(AuthorArray)
    Range("B3").Resize(j, 1).Value = WorksheetFunction.Transpose(BookTypeArray)
    Range("C3").Resize(j, 1).Value = WorksheetFunction.Transpose(TitleArray)
    Range("D3").Resize(j, 1).Value = WorksheetFunction.Transpose(StoreLocationArray)

End If

End Sub