user3045856
user3045856

Reputation:

Saving one specific XML node value with VBS

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

Upvotes: 0

Views: 844

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

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.

Upvotes: 1

Related Questions