【问题标题】:VBA Start at first node if it exists - using If Is NothingVBA 如果存在则从第一个节点开始 - 使用 If Is Nothing
【发布时间】:2015-06-26 10:34:01
【问题描述】:

如何从第一个到最后一个递增每个父节点并应用:

For Each n In XMLFile.SelectNodes("/catalog/book")
    If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
        MsgBox ("banana not here")
    Else
        MsgBox ("banana found")
    End If
Next

香蕉在第一本书中不存在:

?xml version="1.0"?>
<catalog>
<book id="Adventure">
   <author>Ralls, Kim</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>
   <banana>ring</banana>
</book>
<book id="Adventure">
   <author>Ralls, Kim</author>
   <title>Mist</title>
   <price>15.95</price>
   <banana>ring</banana>
</book>
<book id="Mystery">
   <author>Ralls, Kim</author>
   <title>Some Mystery Book</title>
   <price>9.95</price>
   <banana>ring</banana>
</book>
</catalog>

电流输出: “发现香蕉” “发现香蕉” “发现香蕉” “发现香蕉”

【问题讨论】:

    标签: xml vba dom xml-parsing


    【解决方案1】:

    您只是从这里的顶级节点再次重复搜索...

    If XMLFile.SelectSingleNode("/catalog/book/banana") Is Nothing Then
    

    ...所以你总是得到第一个香蕉节点。您需要在“n”上操作,而不是“XMLFile”:

    If n.SelectSingleNode("banana") Is Nothing Then
    

    请记住,您正在遍历层次结构。

    【讨论】:

    • 谢谢你..这是一个漫长的夜晚:)
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    相关资源
    最近更新 更多