You can use XML persistence when you need MDriven to store modeled objects in an XML file, and use selfVM.XmlToObjects when you need to import external XML into new model objects.
XML persistence in MDriven
Persistence keeps data available after an application closes or the system restarts. A persistence mapper maps modeled classes and their attributes to a storage format. MDriven includes an XML file persistence mapper alongside SQL Server, MySQL, and SQLite mappers; see Persistence mappers.
For example, a model with a Person class can persist Person objects in an XML file rather than in a relational database.
Import XML into model objects
XML persistence and XML import address different tasks:
| Task | Use | Example |
|---|---|---|
| Store and retrieve application data | XML file persistence mapper | Keep persisted Person objects available between application sessions.
|
| Create objects from received XML | selfVM.XmlToObjects
|
Receive an XML document from another system and create Person objects for processing.
|
XML is a text-based format for storing and transporting structured data. To import XML in a ViewModel, call the XmlToObjects OCL operator.
Map XML structure to associations and attributes
XmlToObjects converts the input XML to JSON and then creates objects based on matching association and attribute names. Class names do not need to match XML element names.
Given this XML:
<root>
<person id="1">
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id="2">
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>
Model an import root class with a single association named root</>. The object at root needs a many-valued association named person</>. The target class needs name and url attributes. The person XML elements create objects in the person association, while name and url provide attribute values.
Use an action expression such as:
vNewImportFile:=selfVM.XmlToObjects(ImportFile, self.XmlInput)
In this example, self.XmlInput contains the XML text, ImportFile is the root class to create, and vNewImportFile receives the created root object.
Inspect converted input during development
Add an optional String attribute named RawJSon to the class used as the import target. When it exists, the import process assigns the converted JSON to that attribute. This lets you inspect how XML elements, repeated elements, and attributes were represented before objects were created.
For the example XML, repeated person elements become a JSON collection, and the XML attribute id="1" becomes "@id": "1".
Matching behavior
The importer silently ignores input members that it cannot match to a modeled attribute or association. It also accepts model attributes and associations that have no corresponding XML data.
For example, if the XML contains <phone> but the Person class has no phone attribute or association, no phone value is imported and no error is raised. Validate imported data in your model when missing or unexpected input must be detected.
Related XML features
Use ViewModelAsXml to render ViewModel data as XML. Use XMI when you need the XML-based model-interchange format supported by MDriven.
See also
