2014-11-18 14 views
5

Ich habe Code geschrieben, um einige Daten aus einer XML-Datei in Excel zu importieren. Es funktioniert so lange, bis es versucht, Attribute zu lesen, die nicht existieren. Sie sind optional in der Datei und ich kann sie nicht hinzufügen, also muss ich sie im Code behandeln.Wie gehe ich mit optionalen XML-Attributen in VBA um?

Ich habe versucht, die Objekte mit If Is Not Nothing zu handhaben, aber das funktioniert nicht, auch nicht If <> "" oder If <> Null ohne Glück.

Wenn mir jemand helfen kann, wäre ich sehr dankbar.

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 

Ein Beispiel XML-Datei (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> 

Antwort

4

Sie sind Objekte und in VBA Sie überprüfen, ob sie sind leer mit folgenden Syntax

( aassigned worden)

If Not (Object Is Nothing) Then

Also wenn Sie überprüfen möchten, ob die Attribute wer sind e abgerufen und von XML zugewiesen, dann könnten Sie:

' 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 

und ich glaube, dies ist das Ergebnis Ihrer nach

sind, ist

enter image description here

+0

Vielen Dank, ich wurde mit 'If Not Nothing' falsch ist . Es funktioniert jetzt perfekt. – sab0tage

+4

@ sab0tage Ich bin froh, dass ich helfen konnte. Auf Stack Overflow sagen wir Danke von [Antworten annehmen, die uns geholfen haben] (http://stackoverflow.com/help/accepted-answer). Es gibt ein grünes Häkchen neben der Antwort auf der linken Seite, die Sie ankreuzen :) –

Verwandte Themen