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

Difference between revisions of "Texo/XML and XMI Serialization"

(New page: __TOC__ == 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 ...)
 
(Introduction)
 
(7 intermediate revisions by one other user not shown)
Line 9: Line 9:
 
* ModelXMLSaver: serializes pojos to XML/XMI
 
* ModelXMLSaver: serializes pojos to XML/XMI
  
== Serializing/Saving in XML ==
+
=== 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 [[Texo/Troubleshooting#Class_not_managed_by_this_ModelResolver|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 [[Texo/Troubleshooting#NPE_when_de-serializing.2Freading_from_XML.2FXMI|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 [[Texo/Download_and_Install#Using_Texo_at_runtime_outside_of_Eclipse.2FOSGI|here]].
 +
 
 +
== 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).
 
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).
 
<source lang="java">private String writeXMI(final List<Object> objects) {
 
<source lang="java">private String writeXMI(final List<Object> objects) {
Line 36: Line 50:
 
To override these options just call the setOptions method with your own option set.
 
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:
 +
<source lang="java">private List<Object> readXML(final String xml) {
 +
    final ModelXMLLoader xmlLoader = new ModelXMLLoader();
 +
    xmlLoader.setLoadAsXMI(true);
 +
    xmlLoader.setReader(new StringReader(xml));
 +
    return xmlLoader.read();
 +
}
 +
</source>
 +
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:
 +
<source lang="java">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);
 +
</source>
 +
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.
 
+
 
+
== JAXB in the future? ==
+
 
+
As a side note, as Texo has a strong annotation focus it is for sure interesting to also add a JAXB annotation model to Texo. Then it can be possible to generate code with JAXB annotations inserted.
+

Latest revision as of 06:15, 29 April 2012

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

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.

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.

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