【问题标题】:Looking to parse the ECB FX rates from their xml feed希望从他们的 xml 提要中解析欧洲央行的外汇汇率
【发布时间】:2016-02-22 16:50:10
【问题描述】:

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

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 位。

【问题讨论】:

    标签: xml parsing lotus-notes domparser


    【解决方案1】:

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

    我编写了自己的实用程序函数来处理 DOM 数据,这是所有 Notes/Domino 程序员都应该容易熟悉的习语。您可以在此处找到它们以与您的代码进行比较。

    【讨论】:

    • 我不是在第一个 IF 中检查 IsNull 吗?谢谢你的链接,我会通读的!
    • 我没有注意到第一个 If 语句中的 isNull 。遵循您的代码有点困难。我已经尝试在我的实用程序函数中保持非常简洁,但我并没有尝试对树进行完整的递归遍历。
    • 我会检查它们,我同意代码是一团糟。我没有编码,我继承了它,现在需要检查在当前结构中似乎难以实现的东西。您提到的一件事绝对正确:节点值似乎为空,这导致了问题。
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 2013-01-16
    • 2012-09-08
    • 1970-01-01
    • 2017-08-10
    • 2018-12-10
    • 1970-01-01
    相关资源
    最近更新 更多