Share via

XML map to import multiple XML data files

Anonymous
2017-01-27T02:46:14+00:00

Hi

I have a few thousand of XML data files(.xml) and I need to merge them into one worksheet in excel. I could import them one by one but it is too time consuming. All the files have the same header but when I try to import all of them together, I'll get this error: "Cannot find an XML map that corresponds to this data."

Could someone teach me how to create a XML map such that I can import all the files at one go?

Thank you.

Sing Yi

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-01-28T06:35:16+00:00

Hi,

Assuming that all files are on desktop, in a folder  fd xml

Option1

import all xml files without xml maps in a new sheet named: append-data

try this code

Sub Load_XML_files()

 'Jan 28, 2017

Const sName$ = "append-data"

Dim wb As Workbook, wb1 As Workbook

Set wb = ThisWorkbook

Dim newSht As Worksheet, sh As Worksheet

Dim sPath

sPath = "C:\Users\Tasos\Desktop\fd xml" '<< change path

Dim sFile

Dim L As Long

Application.ScreenUpdating = False

Application.DisplayAlerts = False

For Each sh In wb.Worksheets

If sh.Name = sName Then sh.Delete

Next

Set newSht = wb.Sheets.Add

newSht.Name = sName

sFile = Dir(sPath & "*.xml")

L = 1

Do Until sFile = ""

Set wb1 = Workbooks.OpenXML(Filename:=sPath & sFile, LoadOption:=xlXmlLoadImportToList)

With newSht

wb1.Sheets(1).UsedRange.Copy .Cells(L, 1)

wb1.Close False

.ListObjects(1).Range.AutoFilter

.ListObjects(1).Unlist

If L > 1 Then .Cells(L, 1).EntireRow.Delete

L = .Cells(Rows.Count, 1).End(xlUp).Row + 1

End With

sFile = Dir()

Loop

newSht.Range("A1").CurrentRegion.Interior.Pattern = xlNone

newSht.ListObjects.Add(xlSrcRange, newSht.Range("A1").CurrentRegion, , xlYes).Name = "Tbl_01"

wb.Save

Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub

XXXXXXXXXXXXXXXXXXXXXXXXXX

Option2

import all xml files with xml maps in a new sheet named: append-data

Sub Load_XML_filesMap()

  'Jan 28, 2017

 Const sName$ = "append-data"

 Dim wb As Workbook

 Set wb = ThisWorkbook

 Dim newSht As Worksheet, sh As Worksheet

 Dim sPath

 sPath = "C:\Users\Tasos\Desktop\fd xml" '<< change path

 Dim sFile

 Dim L As Long

 Application.ScreenUpdating = False

 Application.DisplayAlerts = False

 For Each sh In wb.Worksheets

 If sh.Name = sName Then sh.Delete

 Next

 Set newSht = wb.Sheets.Add

 newSht.Name = sName

'

 'delete old XMLMaps ###

Dim obj As XmlMap

For Each obj In ActiveWorkbook.XmlMaps

obj.Delete

Next obj

' ###

'

 sFile = Dir(sPath & "*.xml")

 L = 1

 Do Until sFile = ""

ActiveWorkbook.XmlImport URL:=sPath & sFile, ImportMap:=Nothing, Overwrite:=True, Destination:=newSht.Cells(L, 1)

L = newSht.Cells(Rows.Count, 1).End(xlUp).Row + 2

 sFile = Dir()

 Loop

 wb.Save

 Application.DisplayAlerts = True

 Application.ScreenUpdating = True

 End Sub

Was this answer helpful?

10+ people found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-02-01T11:23:22+00:00

    Hi

    Using 2010 should not limit you too much, at least not with this specific issue.

    You can download the Power Query add in for Excel 2010 and use very similar functionality through it - the path would be through the add in ribbon and not the data ribbon.

    Link to download site:

    https://www.microsoft.com/en-us/download/details.aspx?id=39379&CorrelationId=4ee33ca4-8a67-459c-86d8-350c94a0af1b

    Eli

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-02-01T02:59:35+00:00

    Hi Eli,

    I'm currently using excel 2010. Thanks for your suggestion! Will consider buying the newer excel if I have more similar cases. :)

    Sing Yi

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-01-30T18:40:41+00:00

    Hi

    Have you tried using Get& Transform to import the XMLs?

    if all your XMLs have similar structure,

    You can go to the data tab and under New Query -> From File -> From Folder  and pick all the XMLs from a single folder.

    If they are different, you can use the 'From XML' connector at the same location and automate to repeat per XML.

    Eli

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-01-27T23:45:47+00:00

    Hi Sing Yi,

    To address this concern you have about importing multiple XML data files, please answer a few questions:

    • Is this the first time you encountered this issue when exporting multiple XML maps?
    • Are you getting any other errors when you use Excel?

    Please refer to this link to learn about how you can import multiple XML data files in Excel.

    Keep us posted.

    Was this answer helpful?

    0 comments No comments