Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Through XML Nodes and Export to Excel

Status
Not open for further replies.

BitNet33

Technical User
Feb 2, 2011
21
CA
Hey All,

So I have a bunch of XML nodes that look like this:
Code:
<TREND_VALUE 
     DATE_TIME="20100729101005" 
     MIN_VALUE="-12" 
     AVG_VALUE="0" 
     MAX_VALUE="0" 
     VALID="TRUE" 
/>
<TREND_VALUE 
     DATE_TIME="20100729102005" 
     MIN_VALUE="-24" 
     AVG_VALUE="-12" 
     MAX_VALUE="0" 
     VALID="TRUE" 
/>
ECT...
And I want to loop through all of them and grab the 5 values inside each node and then put them into their corresponding cells in an Excel worksheet. This is my code so far which isn't working:
Code:
Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = New MSXML2.DOMDocument
Dim xl As Object
Set xl = CreateObject("Excel.Application")
Dim trendValue As As MSXML2.IXMLDOMNodeList
Set trendValue = xmlDoc.getElementsByTagName("TRENDS/TREND/TREND_VALUE")

With xl.Workbooks.Add
        Worksheets.Add.name = "Trend"
        With ActiveSheet.name = "Trend"
          For i = 0 To numTrendValuePerTrend
                ActiveSheet.Cells(3 + i, 1).Value = trendValue.Item(0).Text
                ActiveSheet.Cells(3 + i, 2).Value = trendValue.Item(1).Text
                ActiveSheet.Cells(3 + i, 3).Value = trendValue.Item(2).Text
                ActiveSheet.Cells(3 + i, 4).Value = trendValue.Item(3).Text
                ActiveSheet.Cells(3 + i, 5).Value = trendValue.Item(4).Text
                trendValue = trendValue.NextSibling 
            Next
        End With
I want the values from the nodes to be put in the Excel document starting at row 3 (rows 1&2 are headers) and then place the 5 values in collumns 1-5 and then drop a row and put the data for the next node. This currently isn't working because I'm trying to set "trendValue" equal to it's next sibling, which it won't let me do. Does anyone know the proper way to cycle through siblings and grab their attributes?

Thanks for any help,
BitNet
 
Oh nevermind, I figured it out. I created temporary strings to hold the values of the attributes and then placed them in as I was looping instead of trying to use ".NextSibling". Here's my code for my loop in case anyone is interested:
Code:
Dim date, min, avg, max, valid As String
For i = 0 To numTrendValuePerTrend
    date = trends.Item(i).getAttribute("DATE_TIME")
    min = trends.Item(i).getAttribute("MIN_VALUE")
    avg = trends.Item(i).getAttribute("AVG_VALUE")
    max = trends.Item(i).getAttribute("MAX_VALUE")
    valid = trends.Item(i).getAttribute("VALID")

    ActiveSheet.Cells(3 + i, 1).Value = date
    ActiveSheet.Cells(3 + i, 2).Value = min
    ActiveSheet.Cells(3 + i, 3).Value = avg
    ActiveSheet.Cells(3 + i, 4).Value = max
    ActiveSheet.Cells(3 + i, 5).Value = valid
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top