Skip to content

$Database.ImportFromXml

Overview

DataTable $Database.ImportFromXml ( options )

Imports xml data to specified table. Please see $Datatable.ImportFromXml for more details.

Remarks

This method is a wrapper method for $Datatable.ImportFromXml and DataTable.Save

Examples

Common use case for importing data;

// For following xml structure
// <Root>
//    <Group>
//        <Name>Developers (Junior)</Name>
//    </Group>
// </Root>


 $Database.ImportFromXml({
     Parameters : {
         TargetSchema: 'HR',
         TargetTable: 'Groups',
     },
     XPath : 'Groups/Group'
   });

Customized Column Update;

// Save organization unit positions
$Database.ImportFromXml({
    Parameters : {
        TargetSchema : "HR",
        TargetTable : "OrganizationUnitPositions"
    },
    XPath : "//OrganizationUnitPositions/OrganizationUnitPosition",
    Map : function(xml) {
        // Update position by parent node id
        this.Position = xml.Evaluate('../../Id');
    }
});

Update Only Selected Columns;

$Database.ImportFromXml({
     Parameters : {                                                              // Fetch from Fikstur table under Fikstur schema.
         TargetSchema : 'Maclar',
         TargetTable : 'Fikstur'       
     },
     Columns : [                                                                 // Use only specified columns
       { Name : 'Id' },
       { Name : 'EvSahibiSkor' },
       { Name : 'MisafirSkor' }
     ],
     XPath : '//MacBilgileri',
     Map : function(node) {                                                      // Custom column update
             this.Id = node.Evaluate('Mac/Fikstur');
             this.EvSahibiSkor = node.Evaluate('Skor/EvSahibi');
             this.MisafirSkor = node.Evaluate('Skor/Misafir');
     }
});

Nested Insert and Update;

$Database.ImportFromXml({                                                                        // Save employee
   Parameters : {
       TargetSchema : 'HR',
       TargetTable : 'Employee'
   },
   XPath : 'Identities/Identity',                                                                // Find rows under Identities/Identity xpath
   ColumnsXPath : 'Employee',                                                                    // Fetch column values from Employee. Final xpath
   Map : function(employeeNode) {

       $Database.Get({                                                                           // Fetch matching records from database
           Parameters : {
               TargetSchema : 'HR',
               TargetTable : 'OrganizationUnitPositionMembers'
           },
           Where : {
             Criteria : [
               { Name : 'Employee', Value : employeeNode.Evaluate('Id') },                       // "Employee must equal to Employee/Id xpath value."
               { Name : 'RegistryNumber', Value : '%2', Comparison : 'Like', Condition : 'Or' }  // Another criteria just for sample. "or RegistryNumber must ends with 2"
             ]
           }
       })
       .DeleteAll()                                                                              // Delete existing all rows
       .CreateNew(function() {                                                                   // Create a new row
           this.Employee = employeeNode.Evaluate('Id');                                          // Set Employee column to "Employee/Id" xpath value.
           this.OrganizationUnitPosition = employeeNode.Evaluate('Employee/Position');           // Set OrganizationUnitPosition column to "Employee/Position" xpath value.
       })
       .Save();                                                                                  // Save this table.
  }
});

See Also