PDA

View Full Version : How do I handle missing XML attributes?



sab0tage
11-15-2014, 08:56 AM
Hi,

My code works importing some data from an XML file until one of the optional attributes is missing "getNamedItem("TextDescriptor")". I've tried the use If Is Not Nothing, If <> null and a few other ways to handle the error but it's just not having it.

Does anyone know how to handle it properly?

Regards,
Owen.



Public Sub cmdLoad()

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

'If oAttributes.getNamedItem("TextDescriptor") Is Not Null Then
ActiveSheet.Cells(intI, 2).Value = oSoftkeyDescriptor.Text
'End If

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