Extract Data from an XML File

In this article, we will learn how to extract data from an XML file using the AIMMS XML schema mapping tool.

Generating XSD file

To properly use the XML schema mapping tool in AIMMS, you’ll need an XSD file corresponding to your XML.

If you do not have an XSD file, you can simply use a free online XSD generator tool in most cases. Here, we will use a generator from FreeFormatter.com.

Once you’ve generated the XSD, save it in the root directory of your project.

Creating AIMMS objects

Create the AIMMS objects to be linked with the XML contents.

In this case, we will create the following:

Set S_Meal {
    Index: I_M;
}
StringParameter SP_Price {
    IndexDomain: I_M;
}
StringParameter SP_Desription {
    IndexDomain: I_M;
}
Parameter P_Calories {
    IndexDomain: I_M;
}

Generating the AXM file

AIMMS is equipped with a tool called XML schema mapping.

Using this tool and an XSD file, you’ll be able to generate your own mapping for your XML file and link your data with objects you create in AIMMS.

  1. Access the tool from the menu Tools > XML schema mappings.
  2. Select your XSD file.
  3. If a dialog warns there is no AXM file, click OK.
../../_images/Calque.png

Based on your XSD file, the XML schema mapping tool generates an AXM file of the same name in the root directory of your project (XYZ.xsd will generate XYZ.axm).

This window should now be open:

../../_images/schemaMapping_blank.png

The elements tree of your XML file is displayed on the left.

Data can be stored in an element, in child elements, or in the element’s parameters. In this example there aren’t child elements nor parameters, but the process is the same.

Click on an element or a parameter folder in the elements tree to view a list of attributes.

../../_images/AXMgeneratorBindsTo.png
  • binds-to: This attribute links an element to an index. The data of the element will belong to the chosen index domain. It also links the parent node from the bound element and any child attributes to the index, so their data can be used as parameters of the index based on their maps-to attribute.
  • maps-to: This attribute links an element’s value to an AIMMS identifier (e.g., sets, parameters).
  • read filter: This attribute specifies if you want to read the data of this element or branch. The default value 1 means it will be read. Set it to 0 if you don’t want AIMMS to read the data. When reading an XML with an element bound or mapped to an index/identifier, AIMMS will add the value of this element to your index/identifier.
  • write filter: This attribute specifies if you want to overwrite the data from your XML file using the value of the AIMMS identifier the element has been mapped to. Set the value 0 if you don’t want AIMMS to overwrite, or 1 if you do.

First we need to set the element to bind to the index I_M of the S_Meal set. Here, I_M is bound to the element SP_Name.

Click on the attribute you want to change and enter the value you want to put in the text field. (You can use the wizard tool for auto-completion.)

Click Apply. Because SP_Name is in the same branch as SP_Price, SP_Description, and P_Calories, we have now access to their data to fill S_Meal parameters.

Repeat this process with the other elements and map them to their respective parameters using the maps-to attribute.

Our example has the following mapping:

../../_images/Mapping_final.png

Note

If some objects from your XML aren’t used (parameters or elements), set their read-filter to 0 to avoid error messages.

Reading XML data

Now we can read the data into AIMMS.

Execute the following code in a procedure to obtain your data.

READXML(OutputFile,"NameOfYourAXM.axm");

If you followed the procedure to Extract an XML File from a Server, OutputFile still contains the directory. Otherwise you’ll need to specify the string parameter containing the path to your XML.

Congratulations, you should now have all your XML data accessible in AIMMS!

../../_images/theOtherEndOfTheArticleAsWeKnowIt.png

Example project

You can download the example AIMMS project below: