Mapping XML

Spin can deserialize XML to Java objects and serialize the annotated Java objects to XML by integrating mapping features into its fluent API. JAXB annotations can be added to the involved Java classes to configure the (de-)serialization process but are not required.

Mapping between Representations:

Assume we have a class Customer defined as follows:

@XmlRootElement(name="customer", namespace="http://camunda.org/test")
public class Customer {

  private String name;

  @XmlElement(namespace="http://camunda.org/test")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Mapping XML to Java:

We can map the following XML object

<?xml version="1.0" encoding="UTF-8"?>
<customer xmlns="http://camunda.org/example">
  <name>Kermit</name>
</customer>

to an instance of Customer in the following way:

import static org.eximeebpms.spin.Spin.XML;

String xmlInput = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><customer xmlns=\"http://camunda.org/example\"><name>Kermit</name></customer>";

Customer customer = XML(xmlInput).mapTo(Customer.class);

Type validation

The target type passed to mapTo is, by default, not checked against the process engine’s deserialization type whitelist — that whitelist (deserializationTypeValidationEnabled) guards only ObjectValue process-variable deserialization. mapTo is validated only when the engine additionally sets spinMapToTypeValidationEnabled (see JSON/XML serialized objects using Spin). Where a type name passed to mapTo(String) could originate from data you do not control, prefer a fixed, trusted type.

Mapping Java to XML:

We can map the customer back to XML as follows:

import static org.eximeebpms.spin.Spin.XML;

String xml = XML(customer).toString();

On this page