希望从他们的xml Feed中解析ECB FX汇率

时间:2016-02-22 16:50:10

标签: xml parsing lotus-notes domparser

我正在尝试解析欧洲央行网站的当前汇率。 我目前有以下代码:

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

我的代码中的问题是它并没有真正解析费率。它保存了空值。我对Lotus Notes DomParser并不是很熟悉,而且我越来越难以找到错误。

今天的输出应如下所示:

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);

括号应该是4位小数的计算结果。

1 个答案:

答案 0 :(得分:1)

这在很大程度上是一种猜测,但是当使用NotesDOMParser时,你必须要小心我称之为“幻像节点”,它们基本上是空节点,你不希望找到它们。我看到你的代码正在检查is Nothing,但我的经验是,这并不总是足够的。您可能还需要检查isNull()。至少,这就是我在我使用的代码中一直做的事情。

我编写了自己的utilty函数来处理成语中的DOM数据,这对所有Notes / Domino程序员来说应该是简单易懂的。您可以在此处找到它们以与您的代码进行比较。