使用 VBS 保存一个特定的 XML 节点值 [英] Saving one specific XML node value with VBS

查看:21
本文介绍了使用 VBS 保存一个特定的 XML 节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 代码:

 <组织信息><网络列表><网络><网络数据><RoutingInfoSection><变更历史><ChangeHistoryItem><日期>2013-06-04</日期><描述>BLABLABLA</描述></ChangeHistoryItem><ChangeHistoryItem><日期>2013-05-21</日期><描述>BLABLABLA</描述></ChangeHistoryItem></ChangeHistory></RoutingInfoSection></网络数据></网络></网络列表></组织信息>

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

我的意思是:有些 XML 的第一个位置是最新的日期,有些在最后,有些在中间……我怎样才能获得最新的日期,无论它在哪里?

我想要最近一年的日期(2014 年,如果有 2014 年、2013 年、2012 年、2011 年...11,例如)等最近的一天.

一直关注我的代码:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0") 'Msxml2.DOMDocument/Microsoft.XMLDOMxmlDoc.Async = "假"xmlDoc.setProperty "SelectionLanguage", "XPath"函数ExportaDados对于每个 f 在 fso.GetFolder("C:\Users\f8057612\Desktop\Bancos\Script_Operadoras").Files如果 LCase(fso.GetExtensionName(f)) = "xml" 然后xmlDoc.Load f.Path如果 xmlDoc.ParseError = 0 那么对于 xmlDoc.SelectNodes("//OrganisationInfo/OrganisationName") 中的每个组织信息Operadora = OrganisationInfo.Text温度 = ""对于 xmlDoc.SelectNodes("//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date") 中的每个 Alteracao_Dir如果 Alteracao_Dir.Text <>温度 然后temp = Alteracao_Dir.TextAlteracao = Alteracao_Dir.TextobjetoSaida_Alteracao.WriteLine Operadora &;"&奥特拉考万一temp = Alteracao_Dir.Text下一个下一个WScript.Echo "解析错误:'" &f.路径&"':" &xmlDoc.ParseError.Reason万一万一下一个结束函数

解决方案

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

xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date"设置 mostRecent = 无对于 xmlDoc.SelectNodes(xpath) 中的每个节点如果mostRecent 什么都没有,那么设置 mostRecent = 节点ElseIf node.Text >mostRecent.Text 然后设置 mostRecent = 节点万一下一个如果不是最近的什么都没有那么WScript.Echo "最近的日期是:" &最近的.Text万一

在循环终止后,变量 mostRecentNothing 当没有 节点时,否则它持有 具有最近日期的节点.

I've got the following XML code:

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

I have done a VBScript that is able to read xml files in a directory, get some nodes values and save them to a txt file until now, but I don't want to get all the values in the "Date" Node... My function below saves every value assigned to Operadora and "Alteracao", on "Operadora & ";" & Alteracao", but how can I change my code so it get only the most recent Date that exists?

I mean: Some XML come with the most recent Date in the first position, some of them in the last, and some of them in the middle... How could I get the most recent, wherever it is?

I want the Date with the most recent year (2014, if there are Dates with 2014, 2013, 2012, 2011...), with the most recent month (12, if there are months 12, 06, 08 or 11, for example) and so on with the most recent day.

Follows my code until now:

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

解决方案

Since your date values are in ISO format they can be compared/ordered correctly even as strings, so you could simply do something like this:

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

After the loop terminates the variable mostRecent is Nothing when there wasn't a <Date> node, otherwise it holds the <Date> node with the most recent date.

这篇关于使用 VBS 保存一个特定的 XML 节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆