Spurious
Spurious

Reputation: 2005

Looking to parse the ECB FX rates from their xml feed

I am trying to parse the current exchange rates from the ECB website. I currently have the following code:

Sub walkTree ( node As NotesDOMNode)
    %REM
        This function parses the xml feed to an object
        walkTree runs recursively, meaning that it is
        called only on an NotesDOMNode (a Node) rather than
        on the whole tree!
    %END REM
    Dim child As NotesDOMNode
    Dim elt As NotesDOMNode
    Dim attrs As NotesDOMNamedNodeMap
    Dim a As NotesDOMAttributeNode
    Dim piNode As NotesDOMProcessingInstructionNode
    LF = Chr(13)+Chr(10)

    If Not node.IsNull Then 'If note is not empty
        Select Case node.NodeType   'Switch through NodeType
            'DOMNODETYPE_DOCUMENT_NODE -> Walk child nodes
            'DOMNODETYPE_TEXT_NODE -> Parse text and return currency pare
            'DOMNODETYPE_ELEMENT_NODE -> Usually a node for currency, save the currency and the walk elements to get fx price

        Case DOMNODETYPE_DOCUMENT_NODE:        ' If it is a Document node
            'domParser.Output( "Document node: "+node.Nodename )
            'Set child = node.FirstChild   ' Get the first node
            Dim numChildNodes As Integer
            numChildNodes = node.NumberOfChildNodes
            'domParser.Output(" has "+Cstr(numChildNodes)+" Child Nodes"+LF)

            'Walk child nodes
            While numChildNodes > 0
                If child Is Nothing Then
                    Set child = node.FirstChild   ' Get the first node
                Else
                    Set child = child.NextSibling ' Get next node
                End If
                numChildNodes = numChildNodes - 1
                Call walkTree(child) 'Recursively call this same function on child nodes
            Wend

        Case DOMNODETYPE_DOCUMENTTYPE_NODE:   ' It is a <!DOCTYPE> tag
            'domParser.Output("Document Type node: "+ node.NodeName+LF)

        Case DOMNODETYPE_TEXT_NODE:           ' Plain text node
            'Den Case auskommentieren
            If node.Parentnode.Nodename = "Cube" Or node.Parentnode.Nodename = "Cube time" Then
                'MsgBox CStr(node.NodeValue)
                domParser.Output(node.NodeValue + "##")
            End If

        Case DOMNODETYPE_ELEMENT_NODE:

            If node.NodeName = "Cube" Then 'Müsste heissen "Cube"
                Dim numAttr As Integer
                numAttr = node.Attributes.Numberofentries
                Dim aNode As NotesDOMAttributeNode
                Dim bNode As NotesDOMAttributeNode
                Dim copy As NotesDOMNode
                Set copy = node.Firstchild

                ' Setze attr =  node.Attributes
                ' Zähl die anzahl der Attribute
                ' Wenn es keine hat, dann mache nichts
                ' Wenn es mehr als 0 hat, dann speichere attr(0) und attr(1) in einem array
                ' gebe das array in diesem Sub zurück
                ' parse das array so, dass es im Format CHF/CHF##1.0000; zurückkommt
                domParser.Output(node.NodeValue + "~~")
            End If


            Set elt = node
            Dim numChildren As Integer
            numChildren =  elt.NumberOfChildNodes
            Set child = elt.FirstChild     ' Get child
            While numChildren > 0
                Call walkTree(child)
                Set child = child.NextSibling   ' Get next child
                numChildren = numChildren - 1
            Wend

        Case Else:
            'domParser.Output("Ignoring node: "+Cstr(node.NodeType)+LF)

    End Select  'node.NodeType
    End If        'Not node.IsNull
End Sub

The problem in my code is that it doesn't really parse through the rates. It saves empty values. I am not super familiar with Lotus Notes DomParser and it's getting increasingly difficult for me to find the error.

Today's output should look like this:

CHF/CHF#1.00;USD/CHF#(1.1008/1.1026);GBP/CHF#(1.1008/0.78243);EUR/CHF#1.1008;CNY/CHF#(1.1008/7.1909);

Where the brackets should already be the calculated result with 4 decimals.

Upvotes: 0

Views: 126

Answers (1)

Richard Schwartz
Richard Schwartz

Reputation: 14628

This is largely a guess, but when using the NotesDOMParser you have to be careful about what I call "phantom nodes", which are basically empty nodes where you don't expect to find them. I see that your code is checking is Nothing, but my experience is that that's not always enough. You may also need to be checking isNull(). At least, thats what I've always done in the code that I use.

I wrote my own utilty functions to process DOM data in an idiom that should be easy and familiar to all Notes/Domino programmers. You can find them here for comparison against your code.

Upvotes: 1

Related Questions