I get an XML from website into a string strXML.
Then I create an XML DOM document:
Dim xmlDoc As MSXML2.DOMDocument
Dim xmlElement As MSXML2.IXMLDOMElement
Dim xmlNode As MSXML2.IXMLDOMElement
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.loadXML (strXML)
DisplayNode xmlDoc.childNodes
Now the DisplayNode is a recursive method which calls itself for every line in the XML data:
Public Sub DisplayNode(ByRef Nodes As MSXML2.IXMLDOMNodeList)
Dim xNode As MSXML2.IXMLDOMNode
For Each xNode In Nodes
If xNode.nodeType = NODE_TEXT Then
Debug.Print xNode.parentNode.nodeName & " = " & xNode.nodeValue
Else
If xNode.parentNode.nodeName = "data" Then Debug.Print "*** NEW RECORD ***"
End If
If xNode.hasChildNodes Then
DisplayNode xNode.childNodes
Debug.Print "> recursive call - next field<"
End If
Next xNode
End Sub
The problem here is how to enter the XML data from a recursive loop into a recordest. If it was just a normal loop it would be easy, but a recursive loop cannot keep a truck of which field and which record is being entered as it is continuously passing its parameters.
One way that I can see to do it at the moment is to create a collection of objects which contain two strings. I could add all data nodes to this collection and then use a loop to move data from the collection into a recordset.
However, I wonder if it is possible to read XML string without using a recursive method just plain loops, or perhaps there is a different way of loading a custom XML file/string into a recordset.
This is the output of DisplayNode:
*** NEW RECORD***
EVENTID = 75098
> recursive call <
DESCRIPTION = Pack
> recursive call <
NAME = John Smith
> recursive call <
CUSTOMERID = 37684
> recursive call <
TRADER = MY COMPANY
> recursive call <
ADDRESS = Flat A
SOUTHILL PARK
LONDON
> recursive call <
> recursive call <
*** NEW RECORD***
.
.
.
repeats
EDIT:
Apparently it is possible to pass a reference to a recordset between the recursive calls and the recordset will preserve its state so one by one field can be entered and the record saved. See the full solution below.
Here is a working solution. The method below needs to be in the Access Form which will display the XML data. The text fields in the form should be set that their ‘Contol source’ has the same names as the fields addedd in the ADODB recordset.
The method below enters one by one field into the passed recordset. Because MSXML2 seems to skip empty tags like
<something></something>each tag name with data needs to be checked by name and entered into an appropriate recordset field.