I never did too much with XML, because its kind of native in Excel and there’s plenty of stuff out there already. However, I didn’t like the idea of manipulating data differently if it came in as XML compared to JSON, so I’ve added XML parsing to cJobject. In other words, you can use it to create jSon from XML, and traverse the XML originated object as if it were JSON.
Here’s a one liner to create jSon from XML.
jsonString = xmlStringToJobject (xmlString).stringify
set job = xmlStringToJobject (xmlString)
For more about how this works, see XML to JSON with VBA for a VBA native version. For a little sidebar, I also provided a lazy alternative that delegates the XML to JSON conversion to Google Apps Script by posting XML data to a public GAS webapp and getting back the JSON, but in this article we’ll stick to the VBA native version.
Rest Excel library
This is a large library of APIs that can generally be called as a one liner to populate an Excel Sheet, or otherwise provide structured access to API results. Up till now, it only handled JSON responses, but I’ve added a capability to automatically detect XML and parse that to a cJobject as well.
The open weather API provides both XML and JSON format. In this example, we’ll get the XML version and convert it to JSON inside VBA. Thereafter, we can populate an excel sheet just like every other entry in the REST-EXCEL library.
Here, we provide a column of place names, along with some column headings that correspond to fields in the XML data. The API populates the columns from the XML data after it has converted it to JSON, as below
The library entry
Every API known by the rest-excel library has an entry describing how to process it. Here is the entry for the XML version of the open weather API. This new parameter, resultsFormat with the value erAUTO, indicates that it should detect the format and convert if necessary
With .add(“open weather xml”)
.add “restType”, erQueryPerRow
.add “url”, “http://api.openweathermap.org/data/2.5/weather?q=”
.add “results”, “current”
.add “treeSearch”, True
.add “ignore”, vbNullString
.add “append”, “&mode=xml”
.add “resultsFormat”, erAUTO
This is just pretty much a one liner as usual
With generalDataSetQuery(“weather”, “open weather xml”, “place”)
And that’s all there is to it. You can download this in the cDataSet.xlsm workbook from Excel Liberation, ot if you are feeling more adventurous you can get the code right off the web into your workbook as described here.
For more stuff like this see Excel Liberation