To extract an XML file into Excel, you can either open the XML directly in Excel (which imports it as a table) or use Excel's built-in XML import feature to map XML elements to columns. The fastest method is to open the file with Excel, but for more control over the data structure, use the Developer tab's XML tools. You'll need Excel 2013 or later (Windows or Mac) and the XML file itself. If the XML has a schema (XSD), Excel can use it to automatically map elements; if not, you can let Excel create a schema based on the XML structure.
What You'll Need
- Excel 2013 or later (desktop version; Excel Online has limited XML support).
- The XML file you want to import (with a .xml extension).
- Optional: an XML schema (.xsd) file if you want to pre-define the mapping.
- Basic knowledge of the XML structure (element names and nesting) to help with mapping.
Step-by-Step
Method 1: Open the XML File Directly in Excel
- Open Excel on your computer.
- Go to File > Open and browse to the location of your XML file.
- Change the file type filter from “All Excel Files” to “XML Files (*.xml)” or “All Files (*.*)” so the XML file appears in the list.
- Select the XML file and click Open.
- Excel will show a dialog asking how you want to open the file. Choose As an XML table and click OK.
- If Excel prompts about the schema, select Use the XML Source task pane if you want to manually map elements, or OK to let Excel create a default schema.
- Excel will import the XML data into a new worksheet, with elements as columns and each repeating element as a row.
Method 2: Use the Developer Tab to Import XML
- Enable the Developer tab if it's not visible: right-click anywhere on the ribbon, choose Customize the Ribbon, then check the Developer box in the right pane and click OK.
- Go to the Developer tab and click Source (in the XML group). This opens the XML Source task pane on the right side of the screen.
- Click XML Maps at the bottom of the task pane, then click Add.
- Browse to your XML file and select it. Excel will attempt to create a schema from the XML structure. If the XML has an associated XSD, you can choose that instead.
- Once the map is added, you'll see the XML elements listed in the task pane as a tree structure.
- Drag the elements you want to import from the task pane onto a worksheet. For example, drag the root element to cell A1. Excel will create a table with columns for each child element.
- After dragging, go back to the Developer tab and click Import (in the XML group).
- Select the same XML file and click Import. Excel will populate the table with the data.
Method 3: Use Power Query (Excel 2016 or later)
- Go to the Data tab and click Get Data > From File > From XML.
- Browse to your XML file and click Import.
- In the Navigator window, you'll see the XML structure as a list of tables. Select the table that contains the data you want (usually named after the repeating element).
- Click Load to load the data into a new worksheet, or Transform Data to edit the query before loading.
- If you choose Transform Data, the Power Query editor opens. You can remove columns, change data types, or expand nested elements using the expand icon (double arrow) on column headers.
- Click Close & Load to bring the data into Excel.
Common Mistakes
- Choosing the wrong open option: When opening an XML file, selecting “As a read-only workbook” or “As an XML table” incorrectly can lead to errors or a blank sheet. Always pick “As an XML table” for data import.
- Ignoring the schema: If the XML has a complex structure and you skip the schema, Excel may flatten the data incorrectly or only show the first level of elements.
- Dragging the wrong element: In the XML Source task pane, dragging the root element instead of a repeating child element can result in a single row of data, missing all other records.
- Forgetting to refresh after editing the XML: If you modify the XML file after importing, the Excel table won't update automatically unless you re-import or refresh the connection (if using Power Query).
- Using Excel Online: Excel Online does not support XML import. You must use the desktop version of Excel.
Tips
- Before importing, open the XML file in a text editor (like Notepad) to understand its structure. Look for repeating elements (e.g.,
or - ) – these will become rows in Excel.
- If the XML is very large (over 100 MB), use Power Query instead of the direct open method, as it handles large files more efficiently and allows you to filter data before loading.
- When using the XML Source task pane, you can drag multiple elements to different cells to create separate tables, but ensure each table has a unique name.
- If you need to import the same XML file regularly, consider saving the mapping as an XML Map in Excel. You can reuse it by going to Developer > Source > XML Maps and selecting the saved map.
- For XML files with namespaces (xmlns attributes), Excel handles them automatically, but if you get errors, try removing the namespace declaration from the XML (if you're allowed to modify it) or use Power Query which is more forgiving.
Troubleshooting
Excel says “XML cannot be opened because it contains errors.” This usually means the XML is not well-formed. Open the file in a text editor and check for missing closing tags, mismatched tags, or illegal characters. You can validate the XML using an online validator or a tool like Notepad++ with XML Tools.
The imported data shows only one row or is blank. This often happens when you drag the root element instead of a repeating child element. In the XML Source task pane, expand the tree and drag the element that repeats (e.g.,
Excel imports the data but columns are not in the order you expect. The column order follows the order of elements in the XML file. To reorder, you can either rearrange the XML file (if possible) or use Power Query to reorder columns after loading.
You get a “Schema validation error” when importing. This means the XML does not conform to the schema (XSD) you provided. Check that the XML matches the schema exactly – element names, order, and data types. If you don't have an XSD, let Excel generate one by not supplying a schema.
Power Query cannot find the XML file. Make sure the file path does not contain special characters and that the file is not locked by another program. Also, try refreshing the query after moving the file.
If all else fails: Restart Excel, or try a different method (e.g., if direct open fails, use Power Query). If the XML is corrupted, you may need to obtain a valid version from the source.
Frequently Asked Questions
Can I import an XML file into Excel Online?
No, Excel Online does not support XML import. You need the desktop version of Excel (Windows or Mac).
Can I export Excel data back to XML?
Yes, but only if you have an XML map. Go to Developer > Export, and Excel will export the data according to the map. Without a map, you can use “Save As” and choose XML format, but that may not produce a clean XML structure.
What if my XML has attributes instead of child elements?
Excel handles attributes as columns as well. In the XML Source task pane, attributes appear with a small icon. You can drag them to the worksheet just like elements.
How do I import multiple XML files at once?
Excel doesn't have a built-in batch import. You can use Power Query to combine files from a folder: Data > Get Data > From File > From Folder, then combine the XML files. Or you can write a VBA macro to loop through files.
Why does Excel change my data types (e.g., numbers become text)?
Excel infers data types from the XML schema. If the element is defined as a string in the schema, it will be imported as text. To change, you can modify the schema or use Power Query to set the data type after import.
Can I import XML from a URL?
Yes, in Power Query you can use “From Web” and enter the URL of the XML file. In the classic import, you would need to download the file first.