Skip to content

XML Import and Export

tomvej edited this page Jul 23, 2012 · 1 revision

Parasim uses an own XML mini-framework, which may be utilized to facilitate XML import and export. Typically, during export, one java object (possibly composed of another objects) is transformed into one XML document. Import reverses this process.

Both import and export work with the Document Object Model.

Maven Dependency

To be included in pom.xml.

<dependency>
    <groupId>org.sybila.parasim.model</groupId>
    <artifactId>core</artifactId>
    <version>${parasim.version}</version>
</dependency>

Interface

For a class to be exportable, it has to implement XLMRepresentable interface, which defines toXML method which should create DOM Object representing this instance.

Import requires associated XMLRepresentableFactory. This factory takes a DOM object and creates new instance of given class from it.

The import/export process itself is executed by XMLResource. This interface encapsulates a XML resource (most often in the form of a file) and contains one instance of imported/exported class (which may be manipulated).

The recommended way of using XMLResource is FileXMLResource abstract class (or its parent, StreamXMLResource) which alredy takes care of storing and loading (including verification against XML schema). To work properly, the following methods have to be implemented:

  • getFactory which return a factory associated with target class
  • getSchema which returns path to XML schema associated with target class
  • getNamespace which returns name of XML namespace of XML schema

Composed Classes

Supposed there is a class Container, which contains an instance of class Component. For Container to be imported/exported, both classes have to implement XMLRepresentable and have an associated factory, ContainerXMLFactory and ComponentXMLFactory. Typically, Container.toXML will during its execution call Component.toXML and ContainerXMLFactory will use ComponentXMLFactory. There will be, however, only one XMLResource and associated XML Schema file, ContainerXMLResource whose getFactory method will return an instance of ContainerXMLFactory.

Note on Decimal Numbers

The decimal XML Schema type does not allow floating-point number representation. There are two solutions:

  • element (attribute) type is decimal and toXML method converts floats into fixed-point representation
  • element (attribute) type is string (or token) and getObject method has to control whether the number is correct.