1 Introduction

Mendix is “The App Platform for the Enterprise” and in enterprise software it is not likely that you work in a green field. In almost every situation you will need to integrate with existing systems. Mendix supports many ways of integration but this how-to focuses on how you can import XML documents with Mendix.

This how-to will teach you how to do the following:

  • Prepare the data structure and GUI
  • Add an XML schema
  • Create XML-to-domain mapping

2 Prerequisites

Before you continue, make sure that you know how to create:

3 Preparing the Data Structure and GUI

The XML document used during this how-to contains customers. To be able to see the imported data, you first need to set up the data structure and GUI to maintain the customer data. Then you need to facilitate the uploading and downloading of XML documents. Finally, you will create the actual import logic and the corresponding import mapping.

To prepare the data structure and the GUI, follow these steps:

  1. Create the following Customer entity in your domain model:

  2. Create the overview and detail pages to manage objects of the Customer type.
  3. Create a menu item to access the customer overview page.
  4. Create an entity called XMLDocument that inherits all the properties from System.FileDocument:

  5. Create the overview and detail pages to manage objects of the XMLDocument type.
  6. Create a menu item to access the XML document overview page (for more information, see How to Set Up the Navigation Structure.

4 Adding an XML Schema (XSD)

Whether you plan to import or export documents, working with XML means that your application must contain an XML schema, which is also called an XSD. An XSD describes the possible contents of an XML file. Based on the XSD, your application knows how to read or write an XML file. If you don’t have an XSD file, there are some online XSD generators that accept an XML document as input. For this how-to, you can use Customers.xsd.

To add an XML schema (XSD), follow these steps:

  1. Right-click a module in the Project Explorer and select Add > Document from the menu (you can also use the shortcut Ctrl+N):

  2. Select XML Schema as the document type and click OK:

  3. Enter CustomersXSD as the Name and click OK:

  4. In the XML Schema editor, click Select… and select the XSD file that you downloaded earlier:

  5. Click OK to save the XML schema, which you will be using in the following steps.

5 Create XML-to-Domain mapping

The XML schema describes what the possible contents of an XML file are, but we need to create an XML-to-domain mapping to define how the data in the XML document is imported into the application.

To create the XML-to-domain mapping, follow these steps:

  1. Right-click a module in the Project Explorer and select Add > Document from the menu (you can also use shortcut Ctrl+N):

  2. Select XML-to-Domain mapping as the Document type and click OK:

  3. Enter ImportCustomersMapping as the Name and click OK. The new mapping will open automatically and the elements will be shown.

  4. On the Select XSD Elements dialog box, do the following
    • Select XML schema as the Schema source
    • Select CustomerXSD as the schema
    • Click Expand all to see the tree with elements

  5. Select the following elements: Customer, ID, CompanyName, Address, City, and PostalCode.
  6. Click OK. The first part of the import mapping should look like this:

  7. Open the connector (from the lower-right side of the Modeler or from the View menu):

  8. Drag the entity Customer from the connector to the place-holder in the mapping editor:

    The Map Object Element editor for this element will open automatically:

  9. In the Map Object Element editor, do the following:
    • Select Search for an object as the Find method (to be able to search for an object, you need to define one or more keys in the value-to-attribute mappings)
    • Select Create for If no object was found
    • Select attributes for all five value-to-attribute mappings
    • Select CustomerID as the Key

  10. Click OK to save the mapping.

6 Creating the Import Logic

In this section, you will create the logic to import the customers stored in an XML document in your application.

To create the import logic, follow these steps:

  1. Open the XMLDocument overview page. It should look this, using a default layout:

  2. Right-click the toolbar of the data grid widget and select Add button > Action to add a new action button:

  3. Double-click the new button to open the Edit Action Button editor and do the following:
    • Change the caption to Import XML Document
    • Select Call a microflow as the On click event
    • Click Select for the microflow, create a new microflow, and name it XMLDocument_Import
    • Click OK to save the properties

  4. Right-click the new microflow button and click Go to microflow in the context menu. You should see an empty microflow with an XMLDocument as the input parameter:

  5. Open the Toolbox and drag an Import XML activity to the line between the start and end event. This inserts a new import XML activity.
  6. Double-click the new activity to open the Import XML editor:

  7. Select the input paramater XMLDocument as the XML document.
  8. Select the previously created XML-to-domain mapping ImportCustomersMapping as the mapping.
  9. Click OK to save the properties. The microflow should look like this:

7 Importing an XML File

To import the XML file, follow these steps:

  1. Deploy the application, upload Customers.xml, and trigger the import microflow.
  2. Open the customer overview page and check if the customer data has been imported into your application.