VBA Excel: Loop through XML Attributes

VBA Excel: Loop through XML Attributes



I'm actually building a programme in excel to fill in data into a word document from a webservice.
Because the XML is built up in a strange way, I have to loop through the attributes of a node, to create a list. From this list I will create a table.



My problem is this loop, I'll show you the code:


ConnectionString = "//GetConfigurationItems/ConfigurationItem/AttachmentTypes/AttachmentType"
tblFilter = "1D8651D1-99E2-4D77-9BFF-1A667AA9398D"
id = "14"

Set valuesOXML = CreateObject("msxml2.DOMDocument.4.0")
valuesOXML.LoadXML DMIService.execute(Webservice, functionName, portName, "<![CDATA[<GetConfigurationItems><ConfigurationItem ID=""" & ID & """ Filter=""" & tblFilter & """ Deleted=""0""/></GetConfigurationItems>]]>")

Set fourthNameField = valuesOXML.SelectNodes(ConnectionString)
For Each ftfield In fourthNameField
werte = werte & ftfield.Attributes(0).Text & ";"

Dim x As Integer
For x = 0 To ftfield.Attributes.Item - 1
Debug.Print ftfield.Attributes.Item(x)
Next x
Next ftfield



The command to save the werte variable is working. but the Loop through the attributes fails with the failure-Text:



"Object doens't support this property or method".



The XML looks like this:


<?xml version="1.0"?>
<GetConfigurationItems Error="False">
<ConfigurationItem ID="14" Filter="1D8651D1-99E2-4D77-9BFF-1A667AA9398D" Deleted="0">
<AttachmentTypes DropDownType="14" Filter="1D8651D1-99E2-4D77-9BFF-1A667AA9398D" Deleted="0">
<AttachmentType ShortDesc="BOA_FIT" VersionNo="2" ID="1D8651D1-99E2-4D77-9BFF-1A667AA9398D">FIT</AttachmentType>
</AttachmentTypes>
</ConfigurationItem>
</GetConfigurationItems>






what is ConnectionString? I can't see it in the XML provided. Also, what value is given to tblFilter? used in the LoadXML

– Our Man in Bananas
Sep 7 '18 at 11:46



ConnectionString


tblFilter






AFAIK MSXML2 Version 4.0 is obsolete, try Version 6.0 instead ; cf stackoverflow.com/questions/52221640/…

– T.M.
Sep 7 '18 at 12:00






For x = 0 To ftfield.Attributes.Length - 1

– Axel Richter
Sep 7 '18 at 12:02


For x = 0 To ftfield.Attributes.Length - 1






PS - correct link is Obtain attribute names from XML using VBA

– T.M.
Sep 7 '18 at 12:34






thanks for the link. also very helpfull. and also thanks axel richter, your answer is also the right solution for what i was looking for.. sometimes the solution is so easy :) but i was just stuck somehow..

– LucaS
Sep 7 '18 at 12:38




2 Answers
2



try this (works in VBA using the XML as as string)



I added MSXML2 library as a Reference (Tools > References > Microsoft XML, v6.0)**


Dim valuesOXML As MSXML2.DOMDocument60

Dim ConnectionString As String
Dim fourthNameField As IXMLDOMNodeList
Dim ftfield As IXMLDOMNode
Dim werte As String

ConnectionString = "//GetConfigurationItems/ConfigurationItem/AttachmentTypes/AttachmentType"

Set valuesOXML = CreateObject("Msxml2.DOMDocument.6.0")

valuesOXML.LoadXML DMIService.execute(Webservice, functionName, portName, "<![CDATA[<GetConfigurationItems><ConfigurationItem ID=""" & ID & """ Filter=""" & tblFilter & """ Deleted=""0""/></GetConfigurationItems>]]>")

Set fourthNameField = valuesOXML.SelectNodes(ConnectionString)
For Each ftfield In fourthNameField
werte = werte & ftfield.Attributes(0).Text & ";"

Dim x As Integer
For x = 0 To ftfield.Attributes.Length - 1
Debug.Print ftfield.Attributes.Item(x).Text
Next x
Next ftfield
End Sub






Hi, this is exactly the case.. with For x = 0 To ftfield.Attributes.Length - 1 it is working. thanks a lot. i just started with stackoverflow and this was my first question. im overwhelmed by all the people try to help me so fast! thanks a lot!!

– LucaS
Sep 7 '18 at 12:35







@LucaS: Your're welcome, so long as you ask a good question like that, with example code, clearly explaining the problem, you're likely to get good useful, helpful responses and upvotes, which gives you reputaion

– Our Man in Bananas
Sep 7 '18 at 12:53



Are you not doing the equivalent of the following? Replace my loading from file with your connection code.


Option Explicit
Public Sub testing()
Dim xmlDoc As New MSXML2.DOMDocument60, mainNode As Object, Node As Object, child As Object, child2 As Object
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.Load "C:UsersUserDesktopTest.xml"
Set mainNode = xmlDoc.SelectNodes("//GetConfigurationItems")

For Each Node In mainNode
For Each child In Node.ChildNodes
Debug.Print child.BaseName & " :" & child.Text
For Each child2 In child.ChildNodes
Debug.Print child2.BaseName & " : " & child2.Text
Next
Next
Next Node
End Sub



With attributes:


Option Explicit
Public Sub testing()
Dim xmlDoc As New MSXML2.DOMDocument60, mainNode As Object, Node As Object, child As Object, child2 As Object, i As Long
Set xmlDoc = New MSXML2.DOMDocument60
xmlDoc.Load "C:UsersUserDesktopTest.xml"
Set mainNode = xmlDoc.SelectNodes("//GetConfigurationItems")

For Each Node In mainNode
For Each child In Node.ChildNodes
If child.Attributes.Length > 0 Then
For i = 0 To child.Attributes.Length - 1
Debug.Print child.BaseName & " : " & child.Attributes(i).nodeName & " - " & child.Attributes(i).NodeValue; ""
Next
End If
For Each child2 In child.ChildNodes
If child2.Attributes.Length > 0 Then
For i = 0 To child2.Attributes.Length - 1
Debug.Print child2.BaseName & " : " & child2.Attributes(i).nodeName & " - " & child2.Attributes(i).NodeValue
Next
End If
Next
Next
Next Node
End Sub






Thanks for your reply, but its not what a try to achieve.. i have to get a list with all the attributes from a node.. but thanks for your help anyway!! the other guys gave me the solution :)

– LucaS
Sep 7 '18 at 12:33






oops... my bad... so you wanted a list of the attrbibutes and their values? Or just the attributes?

– QHarr
Sep 7 '18 at 12:34






first just the attribute names.. because im gonna create a table out of the list of attributes.. later i will also need to get the values. but thats not the problem of today :)

– LucaS
Sep 7 '18 at 12:37






I updated with some attributes code.

– QHarr
Sep 7 '18 at 12:39



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)