我有以下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>
我已经完成了一个VBScript,它能够读取目录中的xml文件,获取一些节点值并将它们保存到txt文件,直到现在,但我不想获取“Date”节点中的所有值... 下面的这个功能保存了分配给Operadora和“Alteracao”的每个值,“Operadora&amp;”;“&amp; Alteracao”
如何更改我的代码,以便只获得第一个存在的日期?
遵循适用于我的代码的功能:
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
答案 0 :(得分:0)
使用SelectSingleNode
而不是迭代SelectNodes
的结果:
xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date"
Set Alteracao_Dir = xmlDoc.SelectSingleNode(xpath)
...
或修改XPath表达式以仅匹配第一个节点:
xpath = "//RoutingInfoSection/ChangeHistory/ChangeHistoryItem/Date[1]"
For Each Alteracao_Dir In xmlDoc.SelectNodes(xpath)
...
Next