Recuperer tout les données de fichier xml dans une feuile excel

blalaa Messages postés 167 Date d'inscription   Statut Membre Dernière intervention   -  
blalaa Messages postés 167 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

je souhaite recuperer des données a partir d'un xml et les stoker sur une feuile excel

j'ai trouve ce code mais je ne sais pas comment ca marche

Dim wks As Worksheet

Private Sub BrowseChildNodes(root_node As IXMLDOMNode)
Dim i As Long
Dim c As Long
Dim rng As Range

For i = 0 To root_node.ChildNodes.Length - 1
If root_node.ChildNodes.Item(i).NodeType <> 3 Then
If wks.UsedRange.Cells.Count = 1 Then
Set rng = wks.Cells(1)
Else
Set rng = wks.Cells(wks.UsedRange.Rows.Count + 1, 1)
End If
With rng
.Value = root_node.ChildNodes.Item(i).BaseName
.Offset(0, 1).Value = root_node.ChildNodes.Item(i).nodeTypeString
.Offset(0, 2).Value = root_node.ChildNodes.Item(i).NodeValue
.Offset(0, 3).Value = root_node.ChildNodes.Item(i).Node
For c = 0 To root_node.ChildNodes.Item(i).Attributes.Length - 1
.Offset(0, c + 4).Value = root_node.ChildNodes.Item(i).Attributes.Item(c).BaseName
.Offset(0, c + 5).Value = root_node.ChildNodes.Item(i).Attributes.Item(c).NodeValue
Next c
End With
End If
BrowseChildNodes root_node.ChildNodes(i)
Next
End Sub

Private Sub BrowseXMLDocument(ByVal filename As String)
Dim xmlDoc As DOMDocument, root As IXMLDOMElement
Dim i As Long
Dim c As Long

Set xmlDoc = New DOMDocument
xmlDoc.async = False
xmlDoc.Load filename
Set root = xmlDoc.DocumentElement
If Not root Is Nothing Then
If wks.UsedRange.Cells.Count = 1 Then
Set rng = wks.Cells(1)
Else
Set rng = wks.Cells(wks.UsedRange.Rows.Count + 1, 1)
End If
With rng
.Value = root.BaseName
.Offset(0, 1).Value = root.nodeTypeString
.Offset(0, 2).Value = root.NodeValue
.Offset(0, 3).Value = root.Text
For c = 0 To root.Attributes.Length - 1
.Offset(0, c + 4).Value = root.Attributes.Item(c).BaseName
.Offset(0, c + 5).Value = root.Attributes.Item(c).NodeValue
Next c
End With
BrowseChildNodes root
End If
wks.Cells(1).EntireRow.Insert xlShiftDown
With wks.Cells(1)
.Value = "baseName"
.Offset(0, 1).Value = "nodeTypeString"
.Offset(0, 2).Value = "nodeValue"
.Offset(0, 3).Value = "text"
c = 1
For i = 4 To wks.UsedRange.Columns.Count - 1 Step 2
.Offset(0, i).Value = "attribute" & c
.Offset(0, i + 1).Value = "Value" & c
c = c + 1
Next i
End With
wks.Rows(1).Font.Bold = True
End Sub

Sub test()
Set wks = Worksheets("Feuil28")
BrowseXMLDocument "C:\Desktop\XML\211_DW10F-TTAP_EURO6.3_20200130_HH_V7_BROUILLON.XML"
End Sub


est il possible de recuperer des données que moi je specifie ?


qlq un peut il maider SVP ?

Merci pour votre aide
A voir également:

3 réponses

blalaa Messages postés 167 Date d'inscription   Statut Membre Dernière intervention  
 
Re

j'ai reussi a mettre un code que j'ai bien compris

Sub test_xml()

Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
.Filters.Clear
.Title = "select a XML File"
.Filters.Add "XML File", "*.xml", 1
.AllowMultiSelect = False


If .Show = True Then
xmlFileName = .SelectedItems(1)


Dim xDoc As Object
Set xDoc = CreateObject("MSXML2.DOMDocument")
xDoc.async = False: xDoc.validateOnParse = False
xDoc.Load (xmlFileName)



Set Productss = xDoc.DocumentElement
i = 2
For Each Products In Productss.ChildNodes
For Each Product In Products.ChildNodes
Debug.Print "id: " & Product.ChildNodes(0).Text
Debug.Print "name: " & Product.ChildNodes(1).Text
Debug.Print "price: " & Product.ChildNodes(2).Text
Debug.Print "quantity: " & Product.ChildNodes(3).Text
Debug.Print "------------------------------------"


Application.Cells(i, 5).Value = Product.ChildNodes(0).Text
Application.Cells(i, 6).Value = Product.ChildNodes(1).Text
Application.Cells(i, 7).Value = Product.ChildNodes(2).Text
Application.Cells(i, 8).Value = Product.ChildNodes(3).Text
'.Range("range")

i = i + 1

Next Product
Next Products
End If

End With

End Sub



mon fichier XML
<?xml version="1.0" encoding="UTF-8"?>
<Productss>
<Products>
     <Product>
     <id>p01</id>
  <name>name 1</name>
  <price>5</price>
  <quantity>777</quantity>
 </Product>
 
 <Product>
     <id>p02</id>
  <name>name 2</name>
  <price>8</price>
        <quantity>778</quantity>  
 </Product>
 
 <Product>
     <id>p03</id>
  <name>name 3</name>
  <price>11</price> 
  <quantity>779</quantity>
 </Product>
</Products>
</Productss>


le code fonctionne super bien,
y a t il un moyen de ne pas preciser les noms de balises (Products,Productss,Product)afin de recuperer exactement les mm données sur un autre fichier qui n'as pas forcement les mm nom de ces balises (mais contient les données recuperes (id,name,price,quantity) ?

Merci d'avance
0