Skip to content

ImportFromXml

Imports XML data into a specified table. For more details, see DataTable.ImportFromXml.

1
$Database.ImportFromXml(query: QueryWithMappings):DataTable

Parameters

QueryWithMappings query
    The query object that defines the import parameters.

Returns

The DataTable instance representing the imported data.

Remarks

This method is a wrapper for the DataTable.ImportFromXml and DataTable.Save methods.

The design of your XML data model and SQL table columns is key for ease of use. When XML node and table column names are identical, this method maps them automatically.

Each node selected by XPath is saved as a row: rows are matched by primary key, so existing rows are updated and new rows are inserted. Running the same import again does not create duplicates.

Map function A map function can be used to transform values before saving, for example, to encrypt a value before saving it to the database. Inside the function, this is the data row and the argument is the XML node of the row.

Info

By default, all matching columns and data model elements are automatically updated by name. If your table columns and data model names are different, you can provide a Map function to map columns to your data model manually.

Assume there are two tables, Corporations and SubCorporations, with a one-to-many relation named SubCorporations from Corporations to SubCorporations. When the table columns have the same names as the XML fields, a sub-query saves each corporation together with its related rows. See $Database.ExportToXml for the matching XML structure.

Warning

Set the relation's update rule to "Cascade" to update related rows with sub-queries.

Examples

Save a repeating section to a table

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Data model:
// <Poll>
//   <Questions>
//     <Question><Id>145</Id><Text>What is your favorite product?</Text></Question>
//     <Question><Id>146</Id><Text>Where did you hear about it?</Text></Question>
//   </Questions>
// </Poll>
$Database.ImportFromXml({
    TargetSchema : 'Poll',
    TargetTable : 'Questions',
    XPath : 'Questions/Question'
});
// each Question node is saved as a row of the Questions table

Transform values before saving

1
2
3
4
5
6
7
8
$Database.ImportFromXml({
    TargetSchema : 'Integration',
    TargetTable : 'Accounts',
    XPath : 'Accounts/Account',
    Map : function (accountNode) {
        this.ApiKey = $Crypto.Encrypt('AccountKeys', this.Id, accountNode.Evaluate('ApiKey'));
    }
});

Set a column from a parent node

1
2
3
4
5
6
7
8
9
$Database.ImportFromXml({
    TargetSchema : 'HR',
    TargetTable : 'OrganizationUnitPositions',
    XPath : 'Positions/Position/Units/Unit',
    Map : function (unitNode) {
        // the Position column comes from the Id of the parent Position node
        this.Position = unitNode.Evaluate('../../Id');
    }
});

Update only selected columns

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$Database.ImportFromXml({
    TargetSchema : 'Poll',
    TargetTable : 'Questions',
    Columns : [
        { Name : 'Id' },
        { Name : 'Content' },
        { Name : 'Type' }
    ],
    XPath : 'Questions/Question',
    Map : function (questionNode) {
        this.Content = questionNode.Evaluate('Text');
        this.Type = questionNode.Evaluate('Type/Code');
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$Database.ImportFromXml({
    TargetSchema : 'HR',
    TargetTable : 'Employee',
    XPath : 'Identities/Identity',
    ColumnsXPath : 'Employee',              // column values are read from Identity/Employee
    Map : function (identityNode) {
        var employeeId = identityNode.Evaluate('Id');

        $Database.Get({
            TargetSchema : 'HR',
            TargetTable : 'OrganizationUnitPositionMembers',
            Where : {
                Criteria : [
                    { Expression : 'Employee', Value : employeeId }
                ]
            }
        })
        .DeleteAll()
        .CreateNew({
            Employee : employeeId,
            OrganizationUnitPosition : identityNode.Evaluate('Employee/Position')
        })
        .Save();
    }
});
1
2
3
4
5
6
7
8
$Database.ImportFromXml({
    TargetSchema : 'Definitions',
    TargetTable : 'Corporations',
    XPath : 'Corporations/Corporation',
    SubQueries : [
        { Name : 'SubCorporations' }
    ]
});

See Also