The following provides examples on how Camunda Spin can be used in the process engine to work with XML data. For illustration purposes, let us assume that a String process variable customer containing XML exists. It has the following content:

Further documentation about the usage of Spin can be found in the .

Expression Language Integration

The Spin entry functions can be used wherever the process engine allows expression language. The following BPMN snippet shows a conditional sequence flow expression based on the customer’s post code:

  1. ...
  2. <sequenceFlow>
  3. <conditionExpression xsi:type="tFormalExpression">
  4. ${XML(customer).xPath("/customer/address/postCode").element().textContent() == "1234"}
  5. </conditionExpression>
  6. </sequenceFlow>
  7. ...

If your variable is already an XML variable value and not a string like in the previous example, you can omit the XML(…) call and directly access the variable:

  1. ...
  2. <sequenceFlow>
  3. <conditionExpression xsi:type="tFormalExpression">
  4. ${customer.xPath("/customer/address/postCode").element().textContent() == "1234"}
  5. </conditionExpression>
  6. </sequenceFlow>

Scripting Integration

Native XML Variable Value

The native variable value for XML makes it possible to easily parse an XML string and wrap it inside an object without the need to have a class representing the XML. Suppose we want to save the XML inside a process variable for later use, we could do the following inside a JavaDelegate:

  1. public class MyDelegate implements JavaDelegate {
  2. @Override
  3. public void execute(DelegateExecution execution) throws Exception {
  4. String xml = "<customer xmlns=\"http:\\/\\/camunda.org/example\" name=\"Jonny\">"
  5. + "<street>12 High Street</street>"
  6. + "<postCode>1234</postCode>"
  7. + "</address>"
  8. + "</customer>";
  9. XmlValue xmlValue = SpinValues.xmlValue(xml).create();
  10. execution.setVariable("customerJonny", xmlValue);
  11. }
  12. }

The call to SpinValues.xmlValue(…).create() will transform the string into a DomXML object wrapped by Spin.

If we wanted to retrieve the XML in another JavaDelegate and, e.g., add some more information, we could do this easily:

  1. public class AddDataDelegate implements JavaDelegate {
  2. @Override
  3. public void execute(DelegateExecution execution) throws Exception {
  4. SpinXmlElement xmlElement = customer.getValue().append(Spin.XML("<creditLimit>1000.00</creditLimit>"));
  5. customer = SpinValues.xmlValue(xmlElement).create();
  6. execution.setVariable("customerJonny", customer);
  7. //<?xml version="1.0" encoding="UTF-8"?><customer xmlns="http:\/\/camunda.org/example" name="Jonny"><address><street>12 High Street</street><postCode>1234</postCode></address><creditLimit xmlns="">1000.00</creditLimit></customer>
  8. }
  9. }

When retrieving the XML value via execution.getVariableTyped() there are two options: serialized and deserialized.Retrieving the variable deserialized by calling either getVariableTyped("name") or getVariableTyped("name", true) the XmlValue contains the wrapped DomXML object to represent the XML data. Calling results in XmlValue containing only the raw string, which is advantageous if you only need the string to pass it to, e.g., another API.

Serializing Process Variables

The following Java code sets a process variable to a Customer object that is serialized using Spin’s XML data format:

  1. Address address = new Address("12 High Street", 1234);
  2. Customer customer = new Customer("jonny", address);
  3. ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("aProcess");
  4. ObjectValue typedCustomerValue =
  5. Variables.objectValue(customer).serializationDataFormat("application/xml").create();
  6. runtimeService.setVariable(processInstance.getId(), "customer", typedCustomerValue);

The decisive statement is

  1. ObjectValue typedCustomerValue =
  2. Variables.objectValue(customer).serializationDataFormat("application/xml").create();

This creates a variable value from the customer object. The invocation serializationDataFormat("application/xml") tells the process engine in which format the variable should be serialized. This name must match the name of a data format known to Spin. For example, application/xml is the name of the built-in XML data format.

Once the variable is set, its serialized value can be retrieved using the type variable API. For example: