Solved

Excel VBA and XML - traversing the nodes

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

895 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

14 Experts available now in Live!

Get 1:1 Help Now