【问题标题】:Saving one specific XML node value with VBS使用 VBS 保存一个特定的 XML 节点值
【发布时间】:2014-01-09 11:58:38
【问题描述】:

我有以下 XML 代码:

   <OrganisationInfo>
       <NetworkList>
          <Network>
             <NetworkData>
                <RoutingInfoSection>
                   <ChangeHistory>
                      <ChangeHistoryItem>
                         <Date>2013-06-04</Date>
                         <Description>BLABLABLA</Description>
                      </ChangeHistoryItem>
                      <ChangeHistoryItem>
                         <Date>2013-05-21</Date>
                         <Description>BLABLABLA</Description>
                      </ChangeHistoryItem>
                   </ChangeHistory>
                </RoutingInfoSection>
             </NetworkData>
          </Network>
       </NetworkList>
   </OrganisationInfo>

到目前为止,我已经完成了一个 VBScript,它能够读取目录中的 xml 文件,获取一些节点值并将它们保存到 txt 文件中,但我不想获取“日期”节点中的所有值...我下面的函数将分配给 Operadora 和“Alteracao”的每个值保存在“Operadora &”;“&Alteracao”上,但是如何更改我的代码以便它只获取存在的最新日期?

我的意思是:一些 XML 带有最新的 Date 在第一位,其中一些在最后,还有一些在中间......我怎么能得到最新的,无论它在哪里?

我想要最近一年的日期(2014,如果有日期为 2014、2013、2012、2011...),最近一个月(12,如果有月份 12、06、08 或11,例如)等等,最近一天。

一直遵循我的代码:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0") 'Msxml2.DOMDocument / Microsoft.XMLDOM    
     xmlDoc.Async = "False"
     xmlDoc.setProperty "SelectionLanguage", "XPath"

            Function ExportaDados
                For Each f In fso.GetFolder("C:\Users\f8057612\Desktop\Bancos\Script_Operadoras").Files
                    If LCase(fso.GetExtensionName(f)) = "xml" Then
                        xmlDoc.Load f.Path

                        If xmlDoc.ParseError = 0 Then
                        For Each OrganisationInfo In xmlDoc.SelectNodes("//OrganisationInfo/OrganisationName")
                            Operadora = OrganisationInfo.Text
                            temp = ""

                            For Each Alteracao_Dir In xmlDoc.SelectNodes("//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date")
                                If  Alteracao_Dir.Text <> temp Then
                                    temp = Alteracao_Dir.Text
                                    Alteracao = Alteracao_Dir.Text
                                    objetoSaida_Alteracao.WriteLine Operadora & ";" & Alteracao
                                End If
                                temp = Alteracao_Dir.Text
                            Next
                        Next
                        WScript.Echo "Parsing error: '" & f.Path & "': " & xmlDoc.ParseError.Reason
                        End If
                     End If
                Next
             End Function

【问题讨论】:

    标签: xml vbscript


    【解决方案1】:

    由于您的日期值是 ISO 格式,它们甚至可以作为字符串进行正确比较/排序,因此您可以简单地执行以下操作:

    xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date"
    
    Set mostRecent = Nothing
    For Each node In xmlDoc.SelectNodes(xpath)
      If mostRecent Is Nothing Then
        Set mostRecent = node
      ElseIf node.Text > mostRecent.Text Then
        Set mostRecent = node
      End If
    Next
    
    If Not mostRecent Is Nothing Then
      WScript.Echo "The most recent date is: " & mostRecent.Text
    End If
    

    循环终止后,当没有&lt;Date&gt; 节点时,变量mostRecentNothing,否则它保存最近日期的&lt;Date&gt; 节点。

    【讨论】:

    • 哇,这种语言让我们可以将字符串与“”操作进行比较? O.O 我会测试它并发布!
    • @CharlieVelez 是的。字符串按 ASCII 字符顺序进行比较,因此为 "1" &lt; "2"。但请注意,这也意味着 "10" &lt; "2"!
    • 是的,但是 2014-03-01 比 2014-10-01 高,不是吗?
    • @CharlieVelez No. "2014-0" &lt; "2014-1",不管第 7 个字符是什么。
    • 要了解 Ansgar 技术的威力/适用性,请查看stackoverflow.com/a/15301674/603855stackoverflow.com/a/18067346/603855
    猜你喜欢
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    相关资源
    最近更新 更多