The XML datatype supports writing XML to Strings, output streams or writers.

Writing to a String:

  1. import static org.camunda.spin.Spin.XML;
  2. import org.camunda.spin.xml.SpinXmlElement;
  3. // Create XML element
  4. SpinXmlElement element = XML("<root id=\"test\"/>");
  5. String xml = element.toString();
  6. String value = element.attr("id").value();

Writing to an Output Stream:

  1. import static org.camunda.spin.Spin.XML;
  2. import org.camunda.spin.xml.SpinXmlElement;
  3. import org.camunda.spin.xml.SpinXmlAttribute;
  4. // Create XML element
  5. SpinXmlElement element = XML("<root id=\"test\"/>");
  6. try {
  7. // Define Output Stream
  8. OutputStream outputStream = System.out;
  9. // Wrap Output Stream in Writer
  10. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
  11. // Write element to stream writer
  12. element.writeToWriter(outputStreamWriter);
  13. // Get attribute from element
  14. SpinXmlAttribute attr = element.attr("id");
  15. // Write attribute value to stream writer
  16. attr.writeToWriter(outputStreamWriter);
  17. // End Output Stream
  18. outputStreamWriter.write("\n");
  19. // Close Output Stream and Writer
  20. outputStreamWriter.close();
  21. } catch (IOException e) {
  22. // Handle exception if needed
  23. }

Write to Writer

  1. import static org.camunda.spin.Spin.XML;
  2. import org.camunda.spin.xml.SpinXmlElement;
  3. import org.camunda.spin.xml.SpinXmlAttribute;
  4. // Create XML element
  5. SpinXmlElement element = XML("<root id=\"test\"/>");
  6. // Create String Writer
  7. StringWriter writer = new StringWriter();
  8. // Write element to Writer
  9. element.writeToWriter(writer);
  10. // Get attribute from element
  11. SpinXmlAttribute attr = element.attr("id");
  12. // Write attribute value to Writer
  13. attr.writeToWriter(writer);

原文: https://docs.camunda.org/manual/7.9/reference/spin/xml/03-writing-xml/