Convert xml to Excel from Basics to Advanced
To extract xml to Excel follow any of the below manual or automation (VBA Macro) techniques explained.
By this, you can create your own Excel XML viewer. We have summarized 2 manual methods & 3 different methods using VBA. Be it on-demand process or automation, this complete guide about converting a xml file to Excel will be a handy quick reference.
1. How to Open xml File in Excel?
Follow below steps that will guide to convert XML to Excel quickly right from the menu options.
- Open a Excel workbook.
- Click ‘File’ menu & choose ‘Open’ (shortcut – ‘Alt+f+o’).
- Browse folder path & choose xml file.
- Click ‘Open’ or press ‘Enter’.
- Choose ‘As an XML table’ in popup menu & click ‘Ok’.
- Click ‘Ok’ when Excel notify that a schema will be created based on xml source data.
Now, the system will start to import xml to Excel in active worksheet.
2. Data Import Xml to Excel
To view XML in proper readable format, you can open xml file in Excel using “Data Import” feature as explained below.
- Click ‘Data’ menu & choose ‘From Other Sources’ (shorcut – ‘Alt+a+f+o’)
- Select “From XML Data Import”.
- Choose xml path and file in ‘Select Data Source’ window.
- Excel will notify about schema creation by itself. Click Ok.
- Choose target cell address to import xml data.
- Click ‘Ok.
Usually in manual options, Excel itself will guide you to further steps to import xml to Excel. These option are built in options of Microsoft xml converter.
It is also possible to import data in XML to Excel through macro Code. We have explained 3 different methods in this article to this through VBA.
Xml to Excel Converter Using VBA
1. Quick Convert xml to Excel VBA
This methods import and open xml file in Excel from local path, but it is slightly different from the previous methods. This does a plain import in quick way.
Visual Basic12345678910111213141516Public Sub Convert_XML_To_Excel_From_Local_Path()Dim xml_File_Path As StringDim wb As Workbook'Load XML Data into a New Workbook - Code from Officetricks.comApplication.DisplayAlerts = Falsexml_File_Path = ThisWorkbook.Sheets(1).Cells(2, 1)Set wb = Workbooks.OpenXML(Filename:=xml_File_Path)'Copy Content from New workbook to current active Worksheetwb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")'Close New Workbook & Enable Alertswb.Close FalseApplication.DisplayAlerts = TrueEnd Sub
2. Advanced method to Read each node in XML file using VBA
This VBA code will import xml to Excel by reading each element or node in it, using object model. If it is not just an plain import & you want to process each xml node in code, then this is the most preferred method.Visual Basic123456789101112131415161718192021222324252627282930Public Sub Convert_XML_To_Excel_Through_VBA()'Code from Officetricks.com'Add referece from Menu: "Tools -> References -> Microsoft XML Vn.0"Dim iRow As Integer, iCol As IntegerDim xmlDoc As MSXML2.DOMDocument, xmlRoot As MSXML2.IXMLDOMNodeDim xmlNodes As MSXML2.IXMLDOMNode, xmlData As MSXML2.IXMLDOMNodeSet xmlDoc = New MSXML2.DOMDocument'Load & Wait till complete XML Data is loadedxmlDoc.async = FalsexmlDoc.validateOnParse = FalsexmlDoc.Load ("D:\sitemap.xml")'XML Loaded. Now Read Elements One by One into XML DOM ObjectsSet xmlRoot = xmlDoc.DocumentElementSet xmlNodes = xmlRoot.FirstChild'Read XML Data and Load into Excel Sheet by each Node and Chile NodeiRow = 0For Each xmlNodes In xmlRoot.ChildNodesiRow = iRow + 1iCol = 0For Each xmlData In xmlNodes.ChildNodesiCol = iCol + 1ThisWorkbook.Sheets(2).Cells(1, iCol) = xmlData.BaseNameThisWorkbook.Sheets(2).Cells(iRow, iCol) = xmlData.TextNext xmlDataNext xmlNodesEnd Sub
Before executing the code above, just change the path of the input file in the line, xmlDoc.Load. By this method, you will be able to import multiple xml files into Excel.
3. Extract XML using VBA from Website
The above method is used to load data from a local file. Use the below method if you have to import xml data to Excel from a website URL directly.Visual Basic123456789101112131415Public Sub Import_XML_From_URL()'Clear & Setup Output Sheet - Code from Officetricks.comThisWorkbook.Sheets(2).ActivateThisWorkbook.Sheets(2).Cells.SelectSelection.ClearContents'Switch Off Alert Messages while Importing.Application.DisplayAlerts = False'VBA Command To Import XML File from Website to ExcelThisWorkbook.XmlImport URL:="XML_File_Download_URL", ImportMap:= _Nothing, Overwrite:=True, Destination:=ThisWorkbook.Sheets(2).Range("$A$1")Application.DisplayAlerts = TrueEnd Sub
All the methods discussed above can be used to automate the process of converting XML to Excel file from a local computer or from a Website url.
View Xmlmap – Data mapping using Excel
Excel will create a schema by itself once your import xml into Excel. To view the xmlmaps added to the Excel workbook, follow these steps.
- Right click on imported xml table.
- Select ‘XML’ in the popup menu & choose “XML Source”.
- Sidebar on right hand side will appear with source details.
- Edit Map field details in this sidebar.
- Click on ‘XML Maps’ to view list of maps linked.
- Rename, Add, Delete, Assign map schema details in this window.
The same option is also available in Developer Tab “Document Panel” from the Excel Menu. Use this option to assign a new schema or map to the imported Xml file.
Note: Similar to editing importing xml, you can create a xml file from Excel directly, by saving it in xml format, right from the ‘File’ menu ‘Save As’ Option.
Sitemap to Excel
Bloggers, Web Admin, SEO & Marketting people scan through sitemaps from lot of websites to improve the performance of their pages in search engine result pages. They can use the techniques in this page to import Sitemap from a Website and process all the links within that Domain.
We have completed few quick steps about Extensible Markup Language or XML to Excel converter. This file format is used to exchange data between computer applications across Internet, LAN or apps within same Computer.
This file can be opened in any Internet browsers (Firefox, Chrome, IE, Opera etc.,) or a Text editor directly to view it’s content since it is not a encoded format. But, to view it in a readable format, this article will help more.
Other Reference: Microsoft Answers on Importing XML data
Previous Post: Find Constituency Wise Candidate List – Voting Complete Guide
Next Post: Engineering Unit Converter Download
Convert XML to Excel directly or using VBA