使用VBS保存一个特定的XML节点值

时间:2014-01-09 11:58:38

标签: xml vbscript

我有以下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>

我已经完成了一个能够读取目录中的xml文件的VBScript,获取一些节点值并将它们保存到txt文件,直到现在,但我不想获取“Date”节点中的所有值...下面的我的功能保存了分配给Operadora和“Alteracao”的每个值,“Operadora&amp;”;“&amp; 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

1 个答案:

答案 0 :(得分: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

当循环终止时,如果没有mostRecent节点,变量Nothing<Date>,否则它将保留<Date>节点的最新日期。< / p>

相关问题