Solved

Excel VBA and XML - traversing the nodes

Posted on 2016-07-24
2
77 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

809 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