Full Version: Pulling Attributes using MSXML2
UtterAccess Forums > Microsoft® Access > Access Forms
spdolan
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.
CODE
  
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order id="97693"/>
<Order id="97798"/>
</Orders>

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.
CODE
Public Function GetAttrib(strReturnNew) As Boolean
Dim XMLDoc As New MSXML2.DOMDocument
Dim XMLNode As IXMLDOMNode
Dim XMLNodes As IXMLDOMNodeList
Dim XMLNodeSub As MSXML2.IXMLDOMElement
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
Next

Thanks!
Sean
Doug Steele
Try
trOrderNum = XMLNode.Attributes.getNamedItem("id").Text
spdolan
that gives me a run-time error 91, Object Variable or With Block variable Not Set
Thanks for the quick response, though!
LPurvis
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:
CODE
    For Each XMLNode In XMLNodes
        For Each XMLNodeSub In XMLNode.childNodes
            strOrderNum = XMLNodeSub.Attributes.Item(0).Text
            Debug.Print strOrderNum
        Next
    Next

(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
CODE
    Set XMLNodes = XMLDoc.getElementsByTagName("Order")
    For Each XMLNode In XMLNodes
        strOrderNum = XMLNode.Attributes.Item(0).Text
        Debug.Print strOrderNum
    Next

So that the node list you consider is the Order elements - not the parent Orders.
Cheers.
Doug Steele
Hmm. Not sure, and I'm afraid I'm tied up at the moment.
'll try and get to it this evening.
spdolan
actually, thats the whole return file. not a snippet.
wonder if my strReturnNew isnt getting loaded properly.
LPurvis
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?
spdolan
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.
LPurvis
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?
spdolan
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.
LPurvis
Then the world makes sense once again (for a few minutes anyway ;-)
Cheers.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.