엑셀VBA로 XML 파일 데이터 쉽게 가져오는 방법

초보자도 쉽게 따라 할 수 있는 VBA 코드로 XML 파일 데이터를 가져 오는 샘플 코드 입니다.

샘플코드를 보고 여려가지로 응용해 보세요

 

 

Sub xmlOpen_Click()
    Dim fd As Office.FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
            .Filters.Clear
            .Title = "Select a XML File"
            .Filters.Add "XML File", "*.xml", 1
            .AllowMultiSelect = False
            
        If .Show = True Then
            xmlFileName = .SelectedItems(1)
            'MsgBox xmlFileName
            
            ' Process XML File
            Dim xDoc As Object
            Set xDoc = CreateObject("MSXML2.DOMDocument")
            xDoc.async = False: xDoc.validateOnParse = False
            xDoc.Load (xmlFileName)
            
            ' Get Roor Node
            Set Products = xDoc.DocumentElement
            
            For Each Product In Products.ChildNodes
                Debug.Print "key: " & Product.ChildNodes(0).Text
                Debug.Print "text: " & Product.ChildNodes(1).Text
                Debug.Print "================================"
            Next Product

                         
        End If
    End With