Export XML Documents

Last modified: March 8, 2024

1 Introduction

In enterprise software, it is not likely that you work in a greenfield. 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 export XML documents.

This how-to teaches you how to do the following:

  • Add an XML schema
  • Create domain-to-XML mapping and export logic

2 Prerequisites

Before you can start exporting XML documents, you need data in your application that is used during the export. You first need to set up the data structure and GUI to maintain the customer data. Then, you’ll create the actual export logic and the corresponding export mapping. To do this, you need to know how to do the following:

Before starting this how-to, make sure you have completed the following prerequisites:

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

  2. Create overview and detail pages to manage the Customer objects.

  3. Create a menu item to access the customer overview page.

  4. Create the XMLDocument entity that inherits all the properties from System.FileDocument:

  5. Create a reference set (multiplicity [-]) between XMLDocument and Customer:

3 Adding an XML Schema (XSD)

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

  1. Right-click your module in the App Explorer and select Add other > XML schema.

  2. Enter CustomersXSD for the Name and click OK:

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

  4. Click OK to save the XML Schema. We’ll be using this schema in the following steps.

4 Creating Domain-to-XML mapping

The XML schema describes what the contents of an XML document should be. We need to create domain-to-XML mapping to define how the data in our application is transformed into a XML document.

  1. Right-click your module in the App Explorer and select Add other > Export mapping.

  2. Enter ExportCustomersMapping for the Name:

  3. Click OK, and the Select schema elements for export mapping dialog box will automatically open. Now do the following:

    1. For Schema source, select XML schema.
    2. For the schema, select the previously added CustomersXSD.
    3. In the Schema elements section of the dialog box, click the Expand all and Check all buttons. This automatically selects the Customer element and its child elements. Your screen should now look like this:
  4. Click OK. You should now see the first part of the import mappings:

  5. Open the Connector pane and drag the XMLDocument entity from the Connector into the placeholder:

    The mapping editor for this element will pop up which can be closed by clicking OK.

  6. Drag the Customer entity from the Connector into the placeholder:

    The mapping editor for this element will open up:

  7. In the mapping editor, verify the following:

    • Method is set to By association
    • Association to parent is set to XMLDocument_Customer
  8. Select attributes for all five Attribute to value element mapping instances (or click Map attributes by name). You should have the following mapping:

  9. Click OK to save the mapping.

5 Creating the Export Logic

This section explains how you can create logic to export the customers stored in your application to an XML document.

To create the export logic, follow these steps:

  1. Open the Customer overview page, right-click the toolbar of the data grid widget, and select Add button > Action to add a new Action button:

  2. Double-click the new button to open the properties editor and do the following:

    • For Caption, enter Export as XML
    • For On click, select Call a microflow
    • In the Select Microflow dialog box, click New to create a new microflow and enter Customers_Export for its Name
  3. Click OK to save the button properties.

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

  5. Select the input parameter and delete it.

  6. Open the Toolbox, which should be on the lower-right side of Studio Pro (you can also open it from the View menu).

  7. Drag a Retrieve activity from the Toolbox to the line between the start event and end event.

  8. Double-click the activity to open the Retrieve Objects properties editor and do the following:

    • For Source, select From database
    • For Entity, click Select… and select the customer entity
  9. Click OK. The microflow should now look like this:

  10. Drag a Create object activity from the Toolbox to the line between the start event and end event.

  11. Double-click the activity to open the Create Object editor and do the following:

    • For Entity, select XMLDocument
    • Click New to add a change item
  12. In the Edit Change Item editor, do the following:

    • For the Member, for the change item, select the XMLDocument_Customer reference:
    • For the Value, enter $CustomerList
  13. Click OK to save the change item.

  14. Create a change item to set the Name attribute to ‘customers.xml’ (including the single quotation marks [’]). The Create Object dialog box should now look like this:

  15. Click OK to save the action properties. The microflow should look like this:

  16. Drag an Export with mapping activity from the Toolbox to the line between the start event and end event. This inserts a new export XML activity.

  17. Double-click the new activity to open the properties editor and do the following:

    • For Mapping, select the previously created ExportCustomersMapping XML-to-domain mapping
    • For Parameter type, verify that the entity XMLDocument is automatically selected
    • For the Parameter, select the created NewXMLDocument
    • For the output Name, select the created NewXMLDocument
  18. Click OK to save the properties. The microflow should look like this:

  19. Drag a Download file activity from the Toolbox to the line between the start event and end event.

  20. Double-click the activity to open the Download File dialog box and select NewXMLDocument as the File document:

  21. Click OK. The microflow should now look like this:

  22. Deploy the application and open the customer overview page.

  23. Click the Export as XML button and download the generated XML document.

6 Read More