How to import nested XML files to Magento 2

- Magento 2, Our extensions

Magento 2 Development; Magento 2 tutorial

Since Magento 2 supports only CSV files for data import, you get stuck when your product update is provided via XML. So is a file with customers or any entity that you may need to import to Magento 2. However, some third-party modules understand what XML files are. But it is not always enough. Especially when you deal with complex XML structures or nested XML files

If your XML file contains more than one nested level most third-party extensions cannot import it to Magento 2.  Unless it is the Improved Import & Export Magento 2 module

Below, we explain how to import data to Magento 2 using nested XML files if the update is hidden deep in their structure. To be honest, it is quite simple: you need to specify a path to the data within the file. But let’s first see what nested XML is. 

What is nested XML?

Under nested XML we assume something similar to nested JSON. It is a complex file that consists of nested XML elements where there is a parent element and a child element hidden within it. The structure starts with a root element that can contain multiple other elements that become parent elements for multiple child elements each.

You can use nested XML to provide higher clarity associated with using different layers. Also, nested XML files are associated with better maintenance. However, they are difficult to understand when it comes to Magento 2 import. 

What’s wrong with nested XML files and Magento 2 import? 

Although nested XML files enhance usability and maintenance, they become more complicated to read and understand, especially regarding Magento 2 import. Since each nested object has a unique access path, most Magento 2 import modules cannot read it properly. 

It is also necessary to mention that the same field name can occur in nested objects in the same document making things even worse. Most modules cannot fetch the necessary data under such circumstances. However, the Improved Import & Export extension knows how to import data to Magento 2 via nested XML files.

How to import nested XML files to Magento 2?

The Improved Import & Export extension provides the ability to import nested XML files to Magento 2. You only need to specify a specific path within the structure of your update file. 

Follow these steps to import nested XML files to Magento 2:

  1. Open the Improved Import & Export extension;
  2. Open a new import job;
  3. Select your import entity;
  4. Configure other job settings;
  5. Proceed to the Import Source section:
    • Choose XML as your import file type:
    • Enable ‘Set path to the entity structure in the file’.
    • Specify the path to the desired data in your nested XML file in the ‘Path to the import entity structure’ field.
      Don’t confuse  ‘Path to the import entity structure’  and ‘File path’. While the former is responsible for the path within your XML file, the latter indicates where the file is situated.
      import source for nested JSON

  6. Continue configuring your import job and save it. 

Below, you can see a small example that illustrates the process. We have the following XML file with a complex structure:

import nested xml magento 2

You should specify /ElementName_1/ElementName_2/Items in the  ‘Path to the import entity structure’ field in the Import Source section to import product information specified in the file:

import nested xml magento 2

Congratulations! Now, you can import nested XML files of any complexity to Magento 2.

In addition to XML files, the Improved Import & Export extension also supports CSV, JSON, XLSX, etc. You can import them from various sources, including Dropbox and Googledrive. The module also lets you create cron-based schedules to import your nested XML or other files automatically. You can use mapping if these files contain third-party attributes! 

Along with traditional ways of importing, the Improved Import & Export Magento 2 extension lets you transfer data from Google Sheets or run API-based import and export processes. Follow this link to learn more about the module: 

Get Improved Import & Export Magento 2 Extension

Also, check our Magento 2 Cookbook for more useful tips.