V_Malkoti
Well-known Member
- Joined
- Jun 10, 2015
- Messages
- 898
I am trying to write code which will help me verify a very large number of XML files. Each of these XML files will have a corresponding XSD which will be provided with it.
So far I was able to write code to create XMLMap, then set XPath (repeatable) of some fields to ranges, and then import the XML to that map.
This works fine with my own test XMLs and I can get the data imported in the spreadsheet.
Now the problem is that the actual XMLs have 50+ data elements and thousands of rows each and there are hundreds of those XMLs. I feel that mapping each data element or attribute in this way (hardcoded) is highly impractical. I've spent past few hours looking for a way to create a mechanism with which mapping elements from any XML with no hardcoding would be possible. But no luck.
so what I'm looking for is
In Excel UI, this mapping can be done by simply dragging and dropping the root element from XML Source pane. But this action does not get recorded in Macro recorder.
It is possible that somebody else asked this question before and got answer, but I have not been able to find such post on MrExcel or any other forum so far.
Any help is appreciated.
So far I was able to write code to create XMLMap, then set XPath (repeatable) of some fields to ranges, and then import the XML to that map.
Code:
set oXMLMap = ActiveWorkbook.XmlMaps.Add(strXSDPath, strRootName)
oXMLMap.Name = strMapName
ActiveSheet.Range("A1").Xpath.SetValue oXMLMap, "/myRootElement/myRepeatingElement/myDataElement1", , true
ActiveSheet.Range("B1").Xpath.SetValue oXMLMap, "/myRootElement/myRepeatingElement/myDataElement2", , true
' and so on
oXMLMap.Import URL:=strXMLPath
This works fine with my own test XMLs and I can get the data imported in the spreadsheet.
Now the problem is that the actual XMLs have 50+ data elements and thousands of rows each and there are hundreds of those XMLs. I feel that mapping each data element or attribute in this way (hardcoded) is highly impractical. I've spent past few hours looking for a way to create a mechanism with which mapping elements from any XML with no hardcoding would be possible. But no luck.
so what I'm looking for is
Code:
(psuedocode)
For Each element (dataElement or dataAttribute) in the map
ActiveSheet.cells(1, i).Xpath.SetValue oXMLMap, element.xpath, , true
i++
next
In Excel UI, this mapping can be done by simply dragging and dropping the root element from XML Source pane. But this action does not get recorded in Macro recorder.
It is possible that somebody else asked this question before and got answer, but I have not been able to find such post on MrExcel or any other forum so far.
Any help is appreciated.
Last edited: