VBA DOM 查找具有属性的特定节点
VBA DOM Find Specific Nodes With Attributes
我有一个很大的 XML 文件,我正在尝试使用 DOM 进行解析。如果节点的父节点包含特定的 "id" 属性,我要做的只是从节点中提取信息。
例如,如果我只想提取包含 "id = Adventure" 的书籍的 TITLE 或 AUTHOR - 我将如何使用 DOM 来实现?
<?xml version="1.0"?>
<catalog>
<book id="Adventure">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
</book>
<book id="Adventure">
<author>Boal, John</author>
<title>Mist</title>
<price>15.95</price>
</book>
<book id="Mystery">
<author>Ralls, Kim</author>
<title>Some Mystery Book</title>
<price>9.95</price>
</book>
</catalog>
现在使用此代码:
Sub mySub()
Dim mainWorkBook As Workbook
Dim XCMFILE As Variant
Dim BookType As String
Dim BookTitle As Variant
Dim Title As String
Dim n As IXMLDOMNode
Dim i As Integer
Set mainWorkBook = ActiveWorkbook
Set XCMFILE = CreateObject("Microsoft.XMLDOM")
XCMFILE.Load (XCMFileName) 'Load XCM File
i = 0
For Each n In XCMFILE.SelectNodes("/catalog/book")
BookType= n.Attributes.getNamedItem("id").Text 'Search for id attribute within node
If BookID = "Adventure" Then
Set BookTitle = = XCMFILE.SelectNodes("/catalog/book/title/text()")
Title = BookTitle(i).NodeValue
mainWorkBook.Sheets("Sheet1").Range("B" & i + 3).Value = BookTitle'Prints values into B column
i = i + 1
EndIf
Next
End Sub
我不知道如何正确递增以只拉出适合我 specification/category 的节点。
感谢任何提示和指点 - 我很感激。
你不需要增加任何东西。只需编写一个只选择您感兴趣的节点的 XPath 查询:
For Each n In XCMFILE.SelectNodes("/catalog/book[@id=""Adventure""]")
' n is a book node with the "id" attribute value "Adventure"
Next
我有一个很大的 XML 文件,我正在尝试使用 DOM 进行解析。如果节点的父节点包含特定的 "id" 属性,我要做的只是从节点中提取信息。
例如,如果我只想提取包含 "id = Adventure" 的书籍的 TITLE 或 AUTHOR - 我将如何使用 DOM 来实现?
<?xml version="1.0"?>
<catalog>
<book id="Adventure">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
</book>
<book id="Adventure">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
</book>
<book id="Adventure">
<author>Boal, John</author>
<title>Mist</title>
<price>15.95</price>
</book>
<book id="Mystery">
<author>Ralls, Kim</author>
<title>Some Mystery Book</title>
<price>9.95</price>
</book>
</catalog>
现在使用此代码:
Sub mySub()
Dim mainWorkBook As Workbook
Dim XCMFILE As Variant
Dim BookType As String
Dim BookTitle As Variant
Dim Title As String
Dim n As IXMLDOMNode
Dim i As Integer
Set mainWorkBook = ActiveWorkbook
Set XCMFILE = CreateObject("Microsoft.XMLDOM")
XCMFILE.Load (XCMFileName) 'Load XCM File
i = 0
For Each n In XCMFILE.SelectNodes("/catalog/book")
BookType= n.Attributes.getNamedItem("id").Text 'Search for id attribute within node
If BookID = "Adventure" Then
Set BookTitle = = XCMFILE.SelectNodes("/catalog/book/title/text()")
Title = BookTitle(i).NodeValue
mainWorkBook.Sheets("Sheet1").Range("B" & i + 3).Value = BookTitle'Prints values into B column
i = i + 1
EndIf
Next
End Sub
我不知道如何正确递增以只拉出适合我 specification/category 的节点。
感谢任何提示和指点 - 我很感激。
你不需要增加任何东西。只需编写一个只选择您感兴趣的节点的 XPath 查询:
For Each n In XCMFILE.SelectNodes("/catalog/book[@id=""Adventure""]")
' n is a book node with the "id" attribute value "Adventure"
Next