Solved

Excel VBA and XML - traversing the nodes

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now