Full Version: Pulling Attributes using MSXML2
UtterAccess Forums > Microsoft® Access > Access Forms
Hey folks
For the past few weeks I have been playing with XML in VBA, with a good kick-start from Leigh and others , and now i am running into issues pulling Attributes.
THere is a sample of the XML I am trying to pull the Attributes from - I hope I am simply missing something easy.
<?xml version="1.0" encoding="UTF-8"?>
<Order id="97693"/>
<Order id="97798"/>

I am trying to pull the ids and populate a table with them.
Here is the code I've been working with - maybe one of you might be able to help me along again.
Public Function GetAttrib(strReturnNew) As Boolean
Dim XMLDoc As New MSXML2.DOMDocument
Dim XMLNodes As IXMLDOMNodeList
Dim x As Long
'Dim strAppAttrib As String
Dim strSQLOrdNum As String
Dim strOrderNum As String
XMLDoc.loadXML strReturnNew
x = 1
Set XMLNodes = XMLDoc.getElementsByTagName("Orders")
For x = 1 To XMLNodes.length
Set XMLNode = XMLNodes(x - 1)
strOrderNum = XMLNode.Attributes.Item.Text
Debug.Print strOrderNum
'strSQLOrdNum = "INSERT INTO tblOrderCheck (OrderNum) VALUES (" & strOrderNum & ");"
'DoCmd.RunSQL strSQLOrdNum

Doug Steele
trOrderNum = XMLNode.Attributes.getNamedItem("id").Text
that gives me a run-time error 91, Object Variable or With Block variable Not Set
Thanks for the quick response, though!
Well, when I read a thread with a title including "Pulling Attributes" I knew I wouldn't have much to offer! ;-) ohyeah.gif
Given the snippet you have, it's hard to know if you have more than one Orders element (or is it the root node which doesn't repeat?)
Assuming the single root, you can go with:
    For Each XMLNode In XMLNodes
        For Each XMLNodeSub In XMLNode.childNodes
            strOrderNum = XMLNodeSub.Attributes.Item(0).Text
            Debug.Print strOrderNum

(i.e. note that you need to examine the child nodes of the Orders node - the list you think you have is a list of Orders elements, it just so happens that there's only one.)
HAs a bit more of a direct root, you could also have
    Set XMLNodes = XMLDoc.getElementsByTagName("Order")
    For Each XMLNode In XMLNodes
        strOrderNum = XMLNode.Attributes.Item(0).Text
        Debug.Print strOrderNum

So that the node list you consider is the Order elements - not the parent Orders.
Doug Steele
Hmm. Not sure, and I'm afraid I'm tied up at the moment.
'll try and get to it this evening.
actually, thats the whole return file. not a snippet.
wonder if my strReturnNew isnt getting loaded properly.
I'm sorry, I don't follow.
Oget what you were saying about it being the file - not a snippet. That's fine and cool and all.
The only thing that was wrong with the loading of strReturnNew was as I explained with the use of the objects.
The code offered would correct that. Did you not try it?
No, I tried both bits of code offered. I can see that strReturnNew is loading properly, but when I mouse over both XMLNode and XMLNodes they are = Nothing.
I'm still none the wiser as to your troubles with the implementation.
If the data is loading into the string variable correctly then, by definition, those objects must have pointed to the correct elements at some stage.
When are you testing them - and are you doing so only using the VBE's preview text?
ltimately - when you run the code, do the values from the XML data appear in the Immediate window as expected?
If not - then what is your full code that you've implemented?
No, thats done it. I got rid of a debug.print that was holding me back and it runs through no problem.
Thanks for your help once again, Leigh.
Then the world makes sense once again (for a few minutes anyway ;-)
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.