Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 49956

Loop through XML file and get nodes

$
0
0
Hi

This is my first post on this website, i've been using the forum alot over the years, but the time has come for me to post a problem that i cant solve myself.

I'm trying to get some data from a XML file, my issue is that the XML file can vary, but the node names will be the same, but the amount of nodes will vary. I've attached a picture of the issue.

The Data does not come in the same order as in the XML. I've attached both the excel and the XML that i i've tested it on.

Img.PNG

Code:

Sub GetAllData()

Dim mainworkbook As Workbook
    Set mainworkbook = ActiveWorkbook
    Set oXMLFile = CreateObject("Microsoft.XMLDOM")
    XMLFileName = ActiveSheet.txtPath.Text
    oXMLFile.Load (XMLFileName)
    Set TitleNodes = oXMLFile.SelectNodes("/MarkupSummary/Markup/Emne/text()")
    Set IntNodes = oXMLFile.SelectNodes("/MarkupSummary/Markup/Forfatter/text()")
    Set SideetiketNodes = oXMLFile.SelectNodes("/MarkupSummary/Markup/Sideetiket/text()")
    Set KommentarerNodes = oXMLFile.SelectNodes("/MarkupSummary/Markup/Kommentarer/text()")
    Set Markup = oXMLFile.SelectNodes("/MarkupSummary")
       
        x = 1
        r = 5
       
        Set list = oXMLFile.SelectNodes("/MarkupSummary")
       
        For i = 0 To (TitleNodes.Length - 1)
 
            Title = TitleNodes(i).NodeValue
            Forfatter = IntNodes(i).NodeValue
            Sideetiket = SideetiketNodes(i).NodeValue
            Kommentarer = KommentarerNodes(i).NodeValue
            Debug.Print i
           
                If Title Like "Bemærkning*" Then
                mainworkbook.ActiveSheet.Range("E" & r).Value = Forfatter
                mainworkbook.ActiveSheet.Range("B" & r).Value = Sideetiket
                mainworkbook.ActiveSheet.Range("C" & r).Value = Title
                mainworkbook.ActiveSheet.Range("D" & r).Value = Kommentarer
                mainworkbook.ActiveSheet.Range("A" & r).Value = x
               
                x = x + 1
                r = r + 1
                End If
               
        Next
    'ActiveSheet.txtPath.Text = ""

End Sub

I hope some of you have the time and to help me.
Attached Files

Viewing all articles
Browse latest Browse all 49956

Trending Articles