Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Texo/XML and XMI Serialization

Introduction

Serialization to and from XML and XMI is very useful when exporting and importing data, integration with other systems and when implementing webservices. Texo supports XML and XMI serialization by making use of EMF as the (de-)serialization library. This means that everything which is supported by EMF is also supported by Texo. The same Resource options and the same mapping from Ecore to XSD.

The XMI/XML serialization is provided by the org.eclipse.emf.texo.xml plugin. The main classes for serialization are:

  • ModelXMLLoader: loads pojos from XML/XMI
  • ModelXMLSaver: serializes pojos to XML/XMI

Note to make use of XMI/XML serialization make sure that you 'touch' the generated ModelPackages before using the runtime Texo layer, see this tip for more information.

Dependencies

For XML/XMI serialization there are 2 additional dependencies which are needed for Texo:

  • org.eclipse.emf.ecore
  • org.eclipse.emf.ecore.xmi

See also here.

Notes

A few things to take into account:

  • to make of Texo XMI/XML serializatio make sure that you 'touch' the generated ModelPackages before using the runtime Texo layer, see this tip for more information.
  • if you have an xml schema as the basis for the model then make sure the schema has a namespace and a target namespace, see this this tip for more information.

Serializing/Saving in XML/XMI

The code snippet below shows how a set of pojos can be serialized to XMI (if you want XML do not call setSaveAsXMI to true).

private String writeXMI(final List<Object> objects) {
    final ModelXMLSaver xmlSaver = new ModelXMLSaver();
    xmlSaver.setSaveAsXMI(true);
    xmlSaver.setObjects(objects);
    final StringWriter sw = new StringWriter();
    xmlSaver.setWriter(sw);
    xmlSaver.write();
    return sw.toString();
}

The output is written to a writer.

The XMLSaver internally uses a XML/XMI Resource from EMF. The XMLSaver offers only a focused API. If you want to integrate deeper with the XMLSaver it probably makes sense to subclass it and override certain methods.

Note the XMLSaver sets a number of default options which make sense in general:

// set default options which ensure that XML schemas are followed
setDefaultOptions(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
setDefaultOptions(XMLResource.OPTION_EXTENDED_META_DATA, true);
setDefaultOptions(XMLResource.OPTION_SCHEMA_LOCATION, true);
setDefaultOptions(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, true);
setDefaultOptions(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, true);

To override these options just call the setOptions method with your own option set.

De-Serializating/Reading from XML/XMI

This code snippet shows how to read from XML/XMI:

private List<Object> readXML(final String xml) {
    final ModelXMLLoader xmlLoader = new ModelXMLLoader();
    xmlLoader.setLoadAsXMI(true);
    xmlLoader.setReader(new StringReader(xml));
    return xmlLoader.read();
}

It is pretty simple, instantiate a ModelXMLLoader, pass the xml and get a set of Texo generated pojos back. The XMLLoader has a number of default options set:

setDefaultOptions(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
setDefaultOptions(XMLResource.OPTION_EXTENDED_META_DATA, true);
setDefaultOptions(XMLResource.OPTION_SCHEMA_LOCATION, true);
setDefaultOptions(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, true);
setDefaultOptions(XMLResource.OPTION_USE_LEXICAL_HANDLER, true);

You can overwrite these by calling setOptions with your own option set.

The XMLLoader internally uses a XML/XMI Resource from EMF. The XMLLoader offers only a focused API. If you want to integrate deeper with the XMLLoader it probably makes sense to subclass it and override certain methods.

Back to the top