J'utilise VBA pour manipuler le fichier XML. MS Offfice - 2013. J'ai ajouté la bibliothèque - Microsoft XML Version6.0. Veuillez trouver ci-dessous le code XML ainsi que le code. J'essaie de récupérer tous les TID dans le XML et d'écrire sur la feuille. Mais selectectslenode() ne récupère que le premier nœud. Qu'est-ce que je fais mal? Y a-t-il un moyen d'utiliser selectectsinglenode pour retourner chaque TID? Veuillez suggérer. Faites-moi savoir si vous avez besoin d'informations supplémentaires.VBA - SelectSingleNode ne récupère que le premier nœud de la liste
<NDA xmlns="http://www.example.com">
<FileHeader>
<Form>7</Form>
<SID>1521</SID>
</FileHeader>
<Subdivision>
<SID>1521</SID>
<CID>200</CID>
<Version>1</Version>
</Subdivision>
<BC>
<BF>
<BElem>
<BFountain>
<BFeature>
<Start>0</Start>
<End>279</End>
<TType>2</TType>
<SLimit>25</SLimit>
<SIT>3</SIT>
<RBTField>0</RBTField>
<RType>1</RType>
<RParam>0</RParam>
<RParamOp>5</RParamOp>
<TID>4015100639</TID>
</BFeature>
<BFeature>
<Start>0</Start>
<End>279</End>
<TType>1</TType>
<SLimit>50</SLimit>
<SIT>3</SIT>
<RBTField>0</RBTField>
<RType>2</RType>
<RParam>0</RParam>
<RParamOp>5</RParamOp>
<TID>2850474662</TID>
</BFeature>
<BFeature>
<Start>0</Start>
<End>279</End>
<TType>1</TType>
<SLimit>25</SLimit>
<SIT>3</SIT>
<RBTField>0</RBTField>
<RType>1</RType>
<RParam>0</RParam>
<RParamOp>5</RParamOp>
<TID>2563719215</TID>
</BFeature>
<BFeature>
<Start>0</Start>
<End>279</End>
<TType>3</TType>
<SLimit>25</SLimit>
<SIT>3</SIT>
<RBTField>0</RBTField>
<RType>1</RType>
<RParam>0</RParam>
<RParamOp>5</RParamOp>
<TID>1962204848</TID>
</BFeature>
</BFountain>
</BElem>
</BF>
</BC>
</NDA>
Voici le code:
Dim xDoc1 As MSXML2.DOMDocument60
Dim xNodeList1 As MSXML2.IXMLDOMNodeList
Dim xNode1 As MSXML2.IXMLDOMNode
Dim xChildNode1 As MSXML2.IXMLDOMNode
Dim xpathToExtractRow1 As String, XMLNamespaces1 As String
Dim wCompareWorksheet As Excel.Worksheet
Dim sFoundNode As MSXML2.IXMLDOMNode
Set xDoc1 = New MSXML2.DOMDocument60
xDoc1.async = False
xDoc1.validateOnParse = False
XMLNamespaces1 = "xmlns:r='http://www.example.com"
xDoc1.Load ("ABCD.xml")
xDoc1.setProperty "SelectionNamespaces", XMLNamespaces1
xDoc1.setProperty "SelectionLanguage", "XPath"
If xDoc1.parseError.ErrorCode <> 0 Then
Set oErr1 = xDoc1.parseError
Debug.Print oErr1.reason
End If
Set xNodeList1 = xDoc1.SelectNodes("/r:NDA/r:BC/r:BF/r:BElem/r:BFountain/r:BFeature")
z=1
For x = 0 To xNodeList1.Length - 1
bFirstChild = True
If xNodeList1.Item(x).HasChildNodes Then
For i = 0 To xNodeList1.Item(x).ChildNodes.Length - 1
Set sFoundNode = xNodeList1.Item(x).ChildNodes(i).SelectSingleNode("/r:NDA/r:BC/r:BF/r:BElem/r:BFountain/r:BFeature/r:TID")
If Not sFoundNode Is Nothing Then
wCompareWorksheet.Cells(z, 1) = x & "," & i
wCompareWorksheet.Cells(z, 4) = sFoundNode.nodeName
wCompareWorksheet.Cells(z, 6) = sFoundNode.Text
z = z + 1
Else
Debug.Print "sFound is nothing"
End If
Next
End If
Next
'selectsinglenode' comme son nom l'indique ne sélectionne qu'un seul nœud (le premier)' selectnodes' est ce que vous voulez dans ce cas. –
Non .. Parce que je n'ai pas mentionné dans la question de code mais je vais faire d'autres manipulations sur le nœud. Je veux un TID à nœud unique pour un traitement ultérieur. – Jay
'selectnodes' renvoie une collection de noeuds - rien ne vous empêche de traiter chacun de ces noeuds individuellement. –