Exporting XML documents
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 export XML documents with Mendix.
Before you can start exporting XML documents, you need data in your application that is used during the export. You first need to setup the data structure and GUI to maintain customer data. Then you’ll create the actual export logic and corresponding export mapping.
Before you continue, make sure that you know how to create:
- Domain models, if you need more info, take a look at this how-to.
- Custom File Document, if you need more info, take a look at this how-to.
- Overview and detail pages, if you need more info, take a look at this how-to.
- Menu items, if you need more info, take a look at this how-to.
- Create the following entity Customer in your domain model.
- Create overview and detail pages to manage Customer objects.
- Create a menu item to access the customer overview page.
- Create an entity XMLDocument that inherits all properties from System.FileDocument.
- Create a reference set (Multiplicity [-]) between XMLDocument and Customer.
2. 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 (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 a module in the project explorer and select Document from the Add menu. You can also use shortcut Ctrl+N.
Select XML Schema as document type and click OK.
Enter CustomersXSD as name and click OK.
In the XML schema editor click Select… 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.
3. Creating a Domain-to-XML mapping
The XML Schema describes what the contents of an XML document should be, but we need to create an Domain-to-XML mapping to define how the data in our application is transformed into a XML document.
Right click a module in the project explorer and select Document from the Add menu (Ctrl+N).
Select Domain-to-XML mapping as document type and click OK.
Enter ExportCustomersMapping as name and click OK. The elements selector will automatically open.
- Select XML schemaas source.
- Select the previously added CustomersXSD as schema. You should now see the following tree with elements:
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:
Click OK. You should now see the first part of the import mapping like this:
Open the connector, it should be in the bottom right corner of the Mendix Modeler.
If you can’t find the connector, you can re-open it from the view menu.
- Drag the entity XMLDocument from the connector to the place holder in the mapping editor. The mapping editor for this element will pop up, you can close that by clicking OK.
Drag the entity Customer from the connector to place holder in the mapping editor.
The mapping editor for this element will open up:
- Verify that the Association to parent setting is set to XMLDocument_Customer, this should happen automatically.
- Select Retrieving associated objects as Find method.
Select attributes for all 5 attribute to value mappings or click Map attributes by Name, you should have to following mapping:
- Click OK to save the mapping.
4. Creating export logic
In this section we’ll explain how you can create logic to export customers stored in your application to an XML document.
Open the Customer overview page. It should look this when using a default layout:
Right click the toolbar of the data grid widget and select Action from the Add menu to add a new Action button.
- Double click the new button to open the properties editor.
- Change the caption to Export as XML.
Select Call a microflow as the On click setting.
- Click Select… and create a new Microflow named Customers_Export.
- 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. It should be on the bottom right of the Mendix Modeler. If you can’t find the toolbox, you can re-open it from the view menu.
- Drag a Retrieve activity from the toolbox to the line between the start and end event.
Double click the activity to open the properties editor.
- Select From database as Source.
- 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 and end event.
- Double click the activity to open the properties editor.
Select the XMLDocument as Entity.
Click New to add a change item.
- Select the reference XMLDocument_Customer as member for the change item.
- Fill in _$CustomerList _as value.
- Click OK to save the change item.
- In the same way, create a change item to set the Name attribute to ‘customers.xml’ (including quote marks).
Click OK to save the action properties. The Microflow should look like this:
- Drag an Export XML activity from the toolbox to the line between the start and end event. This inserts a new export XML activity.
Double click the new activity to open the properties editor:
- Select the previously created XML-to-Domain mapping ExportCustomersMapping as mapping.
- Verify that the entity XMLDocument is automatically selected as parameter type.
- Select the created NewXMLDocument as parameter.
- Select the created NewXMLDocument as output variable.
Click OK to save the properties. The Microflow should look like this:
- Drag an Download file activity from the toolbox to the line between the start and end event.
Double click the actvity to open the properties editor
Select the NewXMLDocument as input file document and click OK. The Microflow should look like this:
- Deploy the application and open the customer overview page.
- Click the Export as XML button and download the generated XML document.