XmlToObjects imports structured XML into matching objects in your model, for developers who need to turn XML received from an external service or file into model data.
What XmlToObjects does
Use XmlToObjects when you have XML that represents data for classes in your model and you want MDriven to create corresponding objects rather than enter each value manually.
For example, assume your model contains a class named Person with these attributes:
| Attribute | Type |
|---|---|
Name
|
String |
Age
|
Integer |
If the incoming XML represents a person named Sandra whose age is 25, XmlToObjects creates a new Person object with these values:
| Object | Attribute | Value |
|---|---|---|
| Person | Name
|
Sandra |
| Person | Age
|
25 |
This makes XML import suitable for data returned by external services, including SOAP services. See Documentation:SOAP the protocol from the stone age for an example of receiving XML from a SOAP response.
Prepare XML that represents the model data
Structure the XML so that it identifies the object data you want to import. The following example illustrates XML corresponding to the Person values above:
<Person>
<Name>Sandra</Name>
<Age>25</Age>
</Person>
The XML values must be suitable for the target attribute types. In this example, Age is an Integer attribute, so the value is 25, not descriptive text such as twenty-five.
Use XmlToObjects in an import flow
- Obtain the XML string. For example, this can be XML returned from a service call.
- Ensure that the XML structure and values correspond to the classes and attributes you intend to import.
- Apply XmlToObjects in the OCL expression or action that handles the import.
- Inspect the created objects and confirm that imported attribute values are correct.
The available OCL operators are shown in the OCL editor. Open the editor and type a class to inspect the operators available in the current context; see Documentation:OCL General Operators.
Example: import a person
Given the following XML:
<Person>
<Name>Sandra</Name>
<Age>25</Age>
</Person>
an XmlToObjects import creates a Person object with Name = "Sandra" and Age = 25.
Validate the result before relying on imported data. In particular, verify that the XML names correspond to the intended model elements and that each value can be interpreted as the target attribute type.
Related XML and conversion operators
XmlToObjects is for importing XML into objects. Use the related operators for other XML and data-conversion tasks:
- Documentation:OCLOperators ViewModelAsXml exports ViewModel content as XML. Its tagged values control XML node names, child-node layout, attributes, and parent values.
- Documentation:OCLOperators ViewModelAsJSon exports ViewModel content as JSON.
- Documentation:OCLOperators Parse is a separate operator; see its page for its documentation status.
- Documentation:OCLOperators strToDateTime converts a string to a DateTime value when you need to prepare a date/time value for your model.
