【问题标题】:How do I handle optional XML attributes in VBA?如何处理 VBA 中的可选 XML 属性?
【发布时间】:2015-01-15 10:15:22
【问题描述】:

我已经编写了一些代码来将 XML 文件中的一些数据导入到 excel 中,它一直工作到它尝试读取不存在的属性;它们在文件中是可选的,我无法添加它们,所以我需要在代码中处理它。

我尝试使用If Is Not Nothing 处理对象,但这不起作用,If <> ""If <> Null 也没有运气。

如果有人能给我任何帮助,我将非常感激。

Public Sub import()

    Dim oDoc As MSXML2.DOMDocument
    Dim fSuccess As Boolean
    Dim oRoot As MSXML2.IXMLDOMNode
    Dim oSoftkey As MSXML2.IXMLDOMNode
    Dim oAttributes As MSXML2.IXMLDOMNamedNodeMap
    Dim oSoftkeyName As MSXML2.IXMLDOMNode
    Dim oSoftkeyDescriptor As MSXML2.IXMLDOMNode
    Dim oSoftkeyStyleName As MSXML2.IXMLDOMNode

    Dim oChildren As MSXML2.IXMLDOMNodeList
    Dim oChild As MSXML2.IXMLDOMNode
    Dim intI As Integer
    On Error GoTo HandleErr

    Set oDoc = New MSXML2.DOMDocument

    oDoc.async = False
    oDoc.validateOnParse = False
    fSuccess = oDoc.Load(ActiveWorkbook.Path & "\keys.xml")

    If Not fSuccess Then
      GoTo ExitHere
    End If

    intI = 2
    ActiveSheet.Cells(1, 1).CurrentRegion.ClearContents
    ActiveSheet.Cells(1, 1) = "Name"
    ActiveSheet.Cells(1, 2) = "TextDescriptor"
    ActiveSheet.Cells(1, 3) = "StyleName"

    ' Get the root of the XML tree.
    ' Set oRoot = oDoc.DocumentElement
    Set oRoot = oDoc.SelectSingleNode("//IMS_Softkeys")

    ' Each IMS_Softkey in IMS_Softkeys
    For Each oSoftkey In oRoot.ChildNodes

      Set oAttributes = oSoftkey.Attributes

      Set oSoftkeyName = oAttributes.getNamedItem("Name")
      Set oSoftkeyDescriptor = oAttributes.getNamedItem("TextDescriptor")
      Set oSoftkeyStyleName = oAttributes.getNamedItem("StyleName")

      ActiveSheet.Cells(intI, 1).Value = oSoftkeyName.Text

      'Can't handle optional attribute "TextDescriptor" or "SoftkeyStyle"
      ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
      ActiveSheet.Cells(intI, 3).Value = oSoftkeyStyleName.Text

      intI = intI + 1
    Next oSoftkey
ExitHere:
    Exit Sub
HandleErr:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume ExitHere
    Resume
End Sub

一个示例 XML 文件 (keys.xml):

<BATCH>
  <IMS_BATCH>
    <IMS_Softkeys>
      <IMS_Softkey Name="Donut" StyleName="Mer-Green-Yellow" TextDescriptor="1 Donut" />
      <IMS_Softkey Name="Hotdog" StyleName="Mer-White-Black" TextDescriptor="11&quot; Hotdog" />
      <IMS_Softkey Name="Coke_Image" TextDescriptor="Coke" />
      <IMS_Softkey Name="DietCoke_Image" StyleName="Style for DietCocaCola" />
    </IMS_Softkeys>
  </IMS_BATCH>
</BATCH>

【问题讨论】:

    标签: xml excel vba error-handling


    【解决方案1】:

    它们是对象,在 VBA 中,您可以使用以下语法检查它们是否为 已分配

    If Not (Object Is Nothing) Then

    因此,如果您想检查是否从 XML 检索和分配属性,那么您可以:

    ' Print only if the `oSoftKeyDescriptor` is not nothing
    If Not (oSoftkeyDescriptor Is Nothing) Then
        ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
    End If
    
    If Not (oSoftkeyStyleName Is Nothing) Then
        ActiveSheet.Cells(intI, 3).Value = oSoftkeyStyleName.Text
    End If
    

    我相信这就是你所追求的结果

    【讨论】:

    • 非常感谢,我用错了If Not Is Nothing。现在完美运行。
    • @sab0tage 我很高兴能帮上忙。在 Stack Overflow 上,我们通过 accepting answers which helped us 表示感谢。您勾选的左侧答案旁边有一个绿色复选标记:)
    猜你喜欢
    • 2011-12-06
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2015-05-19
    • 1970-01-01
    • 2017-04-20
    • 2015-04-12
    相关资源
    最近更新 更多