Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA and XML - traversing the nodes

Posted on 2016-07-24
2
Medium Priority
?
117 Views
Last Modified: 2016-07-25
I'm trying to grab data from an XML document and paste them in a 2D structured Excel sheet. I got most of the way there.

This is where I'm stuck.

As you can see, I can read all the text from elements EXCEPT where the data I need to read is an Attribute of an element. There are two at this stage in the development of my application.

That is,
The element 'tie' where the attribute is 'type', and the element 'tied' under the element 'notations' where the attribute is 'type'.
Where it exists, I want to populate the relevant column in my Sheet2. (Columns H and L)

Can someone help please? (I tried hacking various variations of getAttribute but failed as my XML is pretty basic)

I have attached the relevant files. Only 1 sub in the VBA - edit the location of the XML document.

Thanks.
Sun-King-201607012.xml
Book1-EE-question.xlsm
0
Comment
Question by:hindersaliva
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 41726671
Try this:
            For Each myChild In myNodeNote.ChildNodes
            
                If myChild.nodeName = "rest" Then Cells(iRow, 3).Value = "Rest"
                                
                If myChild.nodeName = "pitch" Then
                    For Each myChildPitchChilds In myChild.ChildNodes
                        If myChildPitchChilds.nodeName = "step" Then Cells(iRow, 4).Value = myChildPitchChilds.Text
                        If myChildPitchChilds.nodeName = "alter" Then Cells(iRow, 5).Value = myChildPitchChilds.Text
                        If myChildPitchChilds.nodeName = "octave" Then Cells(iRow, 6).Value = myChildPitchChilds.Text
                    Next myChildPitchChilds
                End If
                If myChild.nodeName = "notations" Then
                    For Each myChildPitchChilds In myChild.ChildNodes
                        If myChildPitchChilds.nodeName = "tied" Then Cells(iRow, 12).Value = myChildPitchChilds.getAttribute("type")
                    Next myChildPitchChilds
                End If
                
                If myChild.nodeName = "duration" Then Cells(iRow, 7).Value = myChild.Text
                If myChild.nodeName = "tie" Then Cells(iRow, 8).Value = myChild.getAttribute("type")
                If myChild.nodeName = "voice" Then Cells(iRow, 9).Value = myChild.Text
                If myChild.nodeName = "type" Then Cells(iRow, 10).Value = myChild.Text
                If myChild.nodeName = "staff" Then Cells(iRow, 11).Value = myChild.Text
            
            Next myChild

Open in new window

0
 

Author Closing Comment

by:hindersaliva
ID: 41727133
Perfect!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question