Tips & Tricks - Importing XML via Data Transfer

Since XML files can have very different structures, it is essential to write a custom import script for each XML file you import. To this end, Intrexx offers the option of using a corresponding Groovy script as a data source during a data transfer.

Analyze XML Structure

This example uses the export of a base application that contains simple sample data. The data contained therein will be exported as well. To do this, select the "Export with data" option during export.

Unzip the exported ZIP file. Change the ".lax" extension of the included application export to ".zip" and unzip this file as well.

You will then find a folder with the application's GUID.

In this folder, you will find the "data" folder, which contains the application's data in XML format.

The XML files have the same names as the data groups in the application from which the data originates (e.g., "IX_BASIC_DATAGROUP.data"). The structure of the XML file looks something like this:

            <dump>
	<schema>
		...
	</schema>
	<data>
		<r>
			<c n="data-field1-name">1</c>
			<c n="data-field2-name">1</c>
			<c n="data-field3-name">First entry</c>
		</r>
		...
	</data>
</dump>

        

An additional <r> element is added for each data record.

Create a Groovy script

When configuring the data transfer, you must change the data source and the transformation type to "Groovy script."

Click "Next."

At the beginning, Intrexx generates a dummy script that is general in nature and not specifically designed for XML data. This script still needs to be modified. To do this, open the Intrexx Editor by clicking on the corresponding link here.

Replace the existing script with the one below:

            import groovy.xml.XmlParser
import de.uplanet.lucy.server.datatrans.IDataSet
import de.uplanet.lucy.server.datatrans.IDataObject
import de.uplanet.lucy.server.datatrans.table.DefaultDataRecord
import java.text.SimpleDateFormat
import java.util.TimeZone

void open(){}
void close(){}
/**
 * Returns the data set, from which the data will be read when importing from this data source.
 * @return data set
 */
IDataSet getDataSet()
{
    def ds = new DataSet()
    ds.parseXML()
    return ds
}
/**
 * This class is the data source specific data set implementation.
 */
class DataSet implements IDataSet
{
    def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    def fileContents = new File("C://IX_BASIC_DATAGROUP.data").getText("UTF-8")
    def content = new XmlParser().parseText(fileContents)
    def xmlRows = content.data.r
    def rows = []
    def i = 0

    void parseXML(){
        for(xmlRow in xmlRows)
        {
            def row = [:]
            def xmlColumns = xmlRow.c

            for(xmlColumn in xmlColumns)
            {
                def strColumnName =  xmlColumn."@n"
                def strValue            =  xmlColumn.text()
                row.put(strColumnName, strValue)
            }

            rows.add(row)
        }
    }
    IDataObject next(){
        if (i >= rows.size())   return null;
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"))
        def l_record = new DefaultDataRecord(null, [
            "dtedit":java.sql.Timestamp,
            "luserid":java.lang.Integer,
            "dtinsert":java.sql.Timestamp,
            "luseridinsert":java.lang.Integer,
            "strid":java.lang.String,
            "str_title":java.lang.String
        ])
        def row = rows[i]
        l_record.setValue("dtedit", new java.sql.Timestamp(sdf.parse(row.get("dtedit")).getTime()))
        l_record.setValue("luserid", Integer.parseInt(row.get("luserid")))
        l_record.setValue("dtinsert", new java.sql.Timestamp(sdf.parse(row.get("dtinsert")).getTime()))
        l_record.setValue("luseridinsert", Integer.parseInt(row.get("luseridinsert")))
        l_record.setValue("strid", row.get("strid"))
        l_record.setValue("str_title", row.get("str_title"))

        i++;
        return l_record
    }
    void close(){}
}

        

In the script, the "parseXML" method is first added to the "DataSet" class; this method loads the content into a temporary storage area as text, regardless of the actual data type. This method must also be called when creating the DataSet in the "getDataSet" method before the created object is returned.

Adjust the path to the XML file (in this example, "IX_BASIC_DATAGROUP.data") accordingly.

Next, the "next" method is modified to convert the values from the buffer to the actual data type. In this example, the correct Java data types can be found in the XML file itself; the data types for the individual data fields are specified in the schema section.

With this method, the XML file is first loaded entirely into memory and then processed. With large files, this can lead to a memory overflow, which is why more specialized methods should be used in this case.

Save the script by clicking "OK," and then click "Next."

Select the destination for the import here and click "Next."

Select the data group into which the data should be imported. Click "Next."

Finally, the data still needs to be transformed. To do this, the "Use transformation if supported" setting must be enabled here. Click "Next."

Now we just need the Groovy script for the transformation. Open the Intrexx Editor.

The wizard for creating the data transfer has already modified the transformation field accordingly, so a Groovy script can now be entered here as well. The transformation maps data fields from the source to data fields in the destination. If custom IDs are required during import, this is where they can be generated and assigned to the ID field.

This procedure is not necessary for this example. The source and destination fields can be easily mapped here. Replace the existing script with the one below:

            g_destination.setValue("dtedit", g_source.getValue("dtedit"))
g_destination.setValue("luserid", g_source.getValue("luserid"))
g_destination.setValue("dtinsert", g_source.getValue("dtinsert"))
g_destination.setValue("luseridinsert", g_source.getValue("luseridinsert"))
g_destination.setValue("strid", g_source.getValue("strid"))
g_destination.setValue("str_title", g_source.getValue("str_title"))
        

Please check the case sensitivity in the Groovy script if data is not being imported. Depending on the server type, case sensitivity may need to be taken into account both in the source string and in the transformation.