Every now and then a client asks you to import some data. It doesn’t look like a lot of effort at first glance, so you add it as a task and plan to pick it up at the end of the day.
Looking at the sheet later, you realize that there are more than hundred columns that need to be imported into the application. This means manual labour, a lot of it. As every column represents an attribute, creating 100+ new attributes in an import entity would take ages if you did it manually.
The trick is to use XML-to-domain mapping.
We will use XML-to-domain mapping to create a new import entity from an Excel sheet quickly in a semi-automated way.
This how-to will teach you how to do the following:
- Quickly import a large Excel file with a lot of columns
Before starting this how-to, make sure you have completed the following prerequisite:
- Have an app project with the MxModel Reflection and Excel Importer modules installed and configured from the Mendix App Store
3 Modifying Your Excel Sheet
The Excel sheet you receive in this scenario contains almost every country in the world, and for every country, some data is stored. This data has to be imported into your application.
You can find the Excel sheet here: Countries.
You are going to make an XSD schema from the headers in the Excel sheet so that you can import this later into the model.
To modify your Excel sheet, follow these steps:
- Select the header row with all the country names.
Copy and paste them in a new sheet using the transpose function:
Your headers should be listed vertically and your sheet should look like this: Countries Transposed.
We are now ready to add some tags around the column.
Add one column to the left.
Place the following string in cell A1:
<xs:element type="xs:string" name="
Drag it down all the way to the last country.
In cell C1, enter the following string:
Like you did with the previous string, drag it down to the last country. The Excel sheet should now look like this: Countries with Tags.
Now you have to get the three different columns into one column. This is necessary because later on, you want to copy the whole column into an XSD file.
Select cell D1 and type in the following into the formula box:
Drag the cells down like you’ve done with column A1 and C1. Now your column D should have the columns A, B, and C combined into one, and your sheet should look like this: Countries with Tags and Column D.
4 Preparing Your XSD File
We have used some of the basic functionalities of Excel to help us out creating the first part of our XSD structure. For a proper XSD file, we still need to have a header and a footer. This is where your favourite text editor comes in (for example, Brackets).
To prepare your XSD file, follow these steps:
- Open a new file and call it CountriesImport.xsd.
Place this text as the header of your XSD file:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified"> <xs:element name="CountriesImport"> <xs:complexType> <xs:sequence>
Go back to your sheet, copy the content from column D, and paste it underneath the header.
Now it’s time to place the footer, which will complete the XSD file. Place this text as for footer:
</xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Your file should look like this: Country Import.
Don’t forget to save it!
5 Importing into Your Application Project
The XSD file is ready to be imported into the Desktop Modeler. To import it, follow these steps:
Open your app project and create a new XSD schema:
With the new XSD schema created, it’s time to create the XML-to-domain mapping that will perform the trick for you and make your life a bit easier.
Be sure to check all the attributes! After clicking OK, you should see a mapping entity with all your countries.
You will now generate a real entity from it that you can use as an import table for your Excel sheet. Click Map automatically:
There’s your entity!
Go to your domain model, set the entity’s Persistable property to Yes, and the job is done!
To keep your application clean, you can delete the XSD schema and XML-to-domain files from your project.