Hello I am new to programming and i'm studying it. I don't understand xml and VBA but this task has been assigned to me at work. I tried to search all over the internet to find a solution but i can't figure out because of my low knowledge.
My problem is that i need to extract specific values from a file xml in a file excel
<NODE >
<ANOTHER-NODE>
<XXX name1="value" name2="value2" name3="value3"/>
<XXX name1="value" name2="value2" name3="value3"/>
<XXX name1="value" name2="value2" name3="value3"/>
<XXX name1="value" name2="value2" name3="value3"/>
<XXX name1="value" name2="value2" name3="value3"/>
<XXX name1="value" name2="value2" name3="value3"/>
<ANOTHER-NODE2>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
<File N="xyxyxyxy" N1="xyxyxyxy1" N2="xyxyxyxy2" N3="xyxyxyxy3" N4="xyxyxyxy4"/>
This is like the structure that i have, and what i need to extract in the excel are the "name1="value" from all and the N="xyxyxyxy" from all
Private Sub CommandButtonImport_Click()
Dim xmlr As Office.FileDialog
Set xmlr = Application.FileDialog(msoFileDialogFilePicker)
With xmlr
.Filters.Clear
.Title = "Seleziona un File XML"
.Filters.Add "XML File", "*.xml", 1
.AllowMultiSelect = False
If .Show = True Then
XmlFileName = .SelectedItems(1)
Dim xmlDoc As MSXML2.DOMDocument60
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
If xmlDoc.Load(XmlFileName) = True Then
This is the structure that i made, to bring the xml file but i don't know if its right.
P.S this is my first post here i hope it's understandable.
UPDATE
Thanks a lot for the answers, sorry for the xml file, i made it on my own cause the original have sensitive data that i cant share, at the end i found hyour tips reaally helpfull and i wrote the code that do the work, i share
Private Sub CommandButtonImport_Click()
Dim xmlr As Office.FileDialog
Set xmlr = Application.FileDialog(msoFileDialogFilePicker)
With xmlr
.Filters.Clear
.Title = "Seleziona un File XML"
.Filters.Add "XML File", "*.xml", 1
.AllowMultiSelect = False
If .Show = True Then
XmlFileName = .SelectedItems(1)
Dim xmlDoc As MSXML2.DOMDocument60
Dim ECU As MSXML2.IXMLDOMNodeList
Dim File As MSXML2.IXMLDOMNodeList
Dim Feature As MSXML2.IXMLDOMNodeList
Dim NodoLista As MSXML2.IXMLDOMNode
Dim NodoLista1 As MSXML2.IXMLDOMNode
Dim NodoLista2 As MSXML2.IXMLDOMNode
Dim i As Integer
Dim k As Integer
Dim l As Integer
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
If xmlDoc.Load(XmlFileName) = True Then
Set ECU = xmlDoc.SelectNodes("//XXX")
On Error Resume Next
For Each NodoLista In ECU
i = i + 1
With ThisWorkbook.Sheets("Foglio1").Rows(i)
.Cells(1).Value = NodoLista.Attributes(0).NodeValue
End With
Next NodoLista
Set File = xmlDoc.SelectNodes("//File")
On Error Resume Next
For Each NodoLista1 In File
k = k + 1
With ThisWorkbook.Sheets("Foglio1").Rows(k)
.Cells(3).Value = NodoLista1.Attributes(0).NodeValue
End With
Next NodoLista1
Set Feature = xmlDoc.SelectNodes("//Feature")
On Error Resume Next
For Each NodoLista2 In Feature
l = l + 1
With ThisWorkbook.Sheets("Foglio1").Rows(l)
.Cells(5).Value = NodoLista2.Attributes(0).NodeValue
End With
Next NodoLista2
End If
End If
End With
End Sub
If i can ask another thing i would love to update this code, doing another control on the xml, i have values on theattributes that appear more then once, there is a command line to dont print in excel the same attributes more then once ?