Solved

Excel VBA and XML - traversing the nodes

Posted on 2016-07-24
2
90 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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