我想要最近一年的日期(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万一在循环终止后,变量 mostRecent 是 Nothing 当没有 节点时,否则它持有 具有最近日期的节点.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 = NothingFor Each node In xmlDoc.SelectNodes(xpath) If mostRecent Is Nothing Then Set mostRecent = node ElseIf node.Text > mostRecent.Text Then Set mostRecent = node End IfNextIf Not mostRecent Is Nothing Then WScript.Echo "The most recent date is: " & mostRecent.TextEnd IfAfter 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 节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 12:41