If you would like to upgrade to a newer long-term support version of Studio Pro, see Moving from Mendix Studio Pro 8 to 9.
Export XML Documents
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
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. So, you need to know how to do the following:
- Create a domain model (for more information, see How to Create a Basic Data Layer)
- Create a custom file document (for more information, see How to Work with Images and Files)
- Create overview and detail pages (for more information, see How to Create Your First Two Overview and Detail Pages)
- Create menu items, (for more information, see How to Set Up the Navigation Structure)
Before starting this how-to, make sure you have completed the following prerequisites:
-
Create the following Customer entity in your domain model:
-
Create overview and detail pages to manage the Customer objects.
-
Create a menu item to access the customer overview page.
-
Create the XMLDocument entity that inherits all the properties from System.FileDocument:
-
Create a reference set (multiplicity [-]) between XMLDocument and Customer:
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.
-
Right-click your module in the Project Explorer and select Add other > XML schema.
-
Enter CustomersXSD for the Name and click OK:
-
In the XML Schema editor, click Select for XML Schema and select the XSD file that you downloaded earlier:
-
Click OK to save the XML Schema. We’ll be using this schema in the following steps.
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.
-
Right-click your module in the Project Explorer and select Add other > Export mapping.
-
Enter ExportCustomersMapping for the Name:
-
Click OK, and the Select schema elements for export mapping dialog box will automatically open. Now do the following:
- For Schema source, select XML schema.
- For the schema, select the previously added CustomersXSD.
- 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:
- For Schema source, select XML schema.
-
Click OK. You should now see the first part of the import mappings:
-
Open the Connector pane, which should be in the lower-right corner of Studio Pro:
-
Drag the XMLDocument entity from the Connector into the placeholder:
The mapping editor for this element will pop up, you can close that by clicking OK.
-
Drag the Customer entity from the Connector into the placeholder:
The mapping editor for this element will open up:
-
In the mapping editor, verify the following:
- Method is set to By association
- Association to parent is set to XMLDocument_Customer
- Method is set to By association
-
Select attributes for all five Attribute to value element mapping instances (or click Map attributes by name to accomplish this). You should have the following mapping:
-
Click OK to save the mapping.
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:
-
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:
-
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
-
Click OK to save the button properties.
-
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:
-
Select the input parameter and delete it.
-
Open the Toolbox, which should be on the lower-right side of Studio Pro (you can also open it from the View menu).
-
Drag a Retrieve activity from the Toolbox to the line between the start event and end event.
-
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
-
Click OK. The microflow should now look like this:
-
Drag a Create object activity from the Toolbox to the line between the start event and end event.
-
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
-
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
-
Click OK to save the change item.
-
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:
-
Click OK to save the action properties. The microflow should look like this:
-
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.
-
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
-
Click OK to save the properties. The microflow should look like this:
-
Drag a Download file activity from the Toolbox to the line between the start event and end event.
-
Double-click the actvity to open the Download File dialog box and select NewXMLDocument as the File document:
-
Click OK. The microflow should now look like this:
-
Deploy the application and open the customer overview page.
-
Click the Export as XML button and download the generated XML document.