Home > Back-end >  Download multiple xml files from URL to MS Excel
Download multiple xml files from URL to MS Excel

Time:08-16

does someone know how to get multiple xml data to MS Excel? I'm trying to download the xml files from URL to excel, but unfortunately there are multiple files and each file has it's own URL.

https://files.naskorsports.com/xml/products/

Do I have to manually type in each and every URL and "paste" it to Excel or is there a automated method?

Thanks in advance

CodePudding user response:

I have developed a VBA code that download all the xml files and fill a worksheet.

Some keys you must understand:

  • It's a VBA code (programming language)
  • It will download all the files, so it will take a time depending of your internet connection, cpu and ram
  • Some of the xml files have a different number of properties, you have to handle it. As I implemented a generic logic, if a xml has fewer properties or they are in a different order the worksheet will be inconsistently.

Steps

Code

Const BaseUrl = "https://files.naskorsports.com/xml/products/"

Sub Main()

    Dim Matches As Object
    Set Matches = GetFileList()
    
    Dim Row As Integer: Row = 2
    
    Dim objXML As MSXML2.DOMDocument60
    Set objXML = New MSXML2.DOMDocument60
    
    For Each Match In Matches
    
        objXML.LoadXML (GetContent(BaseUrl & Match.SubMatches(0)))
    
        'Fill the columns headers
        If (Row = 2) Then
            With objXML.DocumentElement.FirstChild.ChildNodes
                For ColNum = 1 To 16
                    Worksheets(1).Range(Cols(ColNum - 1) & 1).Value = .Item(ColNum - 1).BaseName
                Next
            End With
        End If
        
        'Fill the data
        With objXML.DocumentElement.FirstChild.ChildNodes
            For ColNum = 1 To 14
                Worksheets(1).Range(Cols(ColNum - 1) & Row).Value = .Item(ColNum - 1).Text
            Next
        End With
        
        Row = Row   1

    Next Match
End Sub

Function GetContent(Url As String) As String
    Debug.Print Url

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", Url, False
        .setRequestHeader "User-Agent", "Mozilla/5.0"
        .send
        GetContent = .responseText
    End With
End Function

Function GetFileList() As Object

    Dim Regex As Object
    
    Set Regex = CreateObject("vbscript.regexp")
    
    With Regex
        .Pattern = ">(.*.xml)<"
        .MultiLine = True
        .Global = True
    End With
    
    Set GetFileList = Regex.Execute(GetContent(BaseUrl))
End Function

Function Cols()
    Cols = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
End Function
  • Related