0

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... This function below saves every value assigned to Operadora and "Alteracao", on "Operadora & ";" & Alteracao"

How can I change my code so it get only the first Date that exists?

Follows the function that works on my code:

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

0

Use SelectSingleNode instead of iterating over the results of SelectNodes:

xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date"
Set Alteracao_Dir = xmlDoc.SelectSingleNode(xpath)
...

or modify the XPath expression to match only the first node:

xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date[1]"
For Each Alteracao_Dir In xmlDoc.SelectNodes(xpath)
  ...
Next
Sign up to request clarification or add additional context in comments.

4 Comments

I hoped it would resolve my problem, but now i have another one: Those who create the XML don't follow a pattern that concerns where the last Date is. 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?
@CharlieVelez For that you need to enumerate all <Date> nodes and remember the one with the most recent date. If you can't get it to work: please post it as a new question.
But I wouldn't want to look at the XML... What do you mean by "enumerate" them? Is there a way to create an idea of regular expression that looks the Date with the most recent year (2014, if there is a Date with 2013), the most recent month (12, if there are 06, 08 or 11, for example) and the most recent day? I will post another question...
Thanks for the help! Here it is: stackoverflow.com/questions/21019605/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.