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.
Map function A map function can be used to transform values before saving, for example, to encrypt a password before saving it to the database. See $Database.ExportToXml for more details.
$Database.ImportFromXml({TargetSchema:'Edoksis',TargetTable:'Accounts',XPath:'Accounts/Account',Map:function(xml){varpass=xml.Evaluate('Password');// if not marked as encrypted (means user has edited the password field) encrypt itif(!pass.startsWith('Enc:'))this.Password=$Crypto.Encrypt($EncryptionPassword,this.Id,xml.Evaluate('Password'));else// otherwise just remove the markthis.Password=pass.substr(4);}});
// Assume this is your XML data// <Root>// <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>// </Root>$Database.ImportFromXml({Parameters:{TargetSchema:'Poll',TargetTable:'Questions'},XPath:'Questions/Question'});// Each "Question" node gets saved into the "Questions" table,// mapping the inner XML content to the related columns on the table.
// Save organization unit positions$Database.ImportFromXml({Parameters:{TargetSchema:"HR",TargetTable:"OrganizationUnitPositions"},XPath:"//OrganizationUnitPositions/OrganizationUnitPosition",Map:function(xml){// Update position by parent node idthis.Position=xml.Evaluate('../../Id');}});
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.
$Database.ImportFromXml({// Save employeeParameters:{TargetSchema:'HR',TargetTable:'Employee'},XPath:'Identities/Identity',// Find rows under Identities/Identity xpathColumnsXPath:'Employee',// Fetch column values from Employee. Final xpathMap:function(employeeNode){$Database.Get({// Fetch matching records from databaseParameters:{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 sample criterion: "or RegistryNumber must end with 2"]}}).DeleteAll()// Delete all existing rows.CreateNew(function(){// Create a new rowthis.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 the table.}});
// Save corporations with the subcorporations$Database.ImportFromXml({Parameters:{TargetSchema:'Document',TargetTable:'Corporations'},XPath:'Corporations/Corporation',SubQueries:[{Name:'SubCorporations'}]});
Assume you have the XML below as your form data, along with two SQL tables,¶
Corporations and SubCorporations , and a one-to-many relation from the Corporations table to the SubCorporations table, also named SubCorporations .
Warning
Don't forget to set this relation's update rule to "Cascade" to update with sub-queries.
If the table columns have the same names as the XML fields, this code lets you save each corporation from the XML data into the SQL table while also saving its related¶