XML serialization options reference and examples

The following table lists properties on a serialization options object of type gw.xml.XmlSerializationOptions. These properties provide serialization options:

Serialization option

Type

Description

Default value

Comments

Boolean

If true, exports each element’s comments.

true

Sort

Boolean

If true, ensures that the order of children elements of each element match the XSD. This is particularly important for sequences. This feature only has an effect on an element if it is based on an XSD type. If the entire graph of XmlElement objects contains no XSD-based elements, this property has no effect. If a graph of XML objects contains a mix of XSD and non-XSD-based elements, this feature only applies to the XSD-based elements. This is true independent of whether the root node is an XSD-based element.

Warning: For large XML objects with many nested layers, sorting requires significant computer resources.

true

XmlDeclaration

Boolean

If true, writes the XML declaration at the top of the XML document.

true

Validate

Boolean

If true, validates the XML document against the associated XSD. This feature only has an effect on an element if it is based on an XSD type. If the entire graph of XmlElement objects contains no XSD-based elements, this property has no effect.

GX models created in Studio cannot be invalid. As a result, they do not need to be validated when serialized to XML.

false

Encoding

Charset

The character encoding of the resulting XML data as a java.nio.charset.Charset object. See discussion after this table for tips for setting this property.

UTF-8 encoding

Pretty

Boolean

If true, Gosu attempts to improve visual layout of the XML with indenting and line separators. If you set this to false, then Gosu ignores the values of the properties: Indent, LineSeparator, AttributeNewLine, AttributeIndent.

true

The following properties provide options that take effect only if the Pretty property is true:

Serialization option

Type

Description

Default value

Indent

String

The String to export for each indent level to make the hierarchy clearer.

Two spaces

LineSeparator

String

The line separator as a String.

The new line character (ASCII 10).

AttributeNewLine

Boolean

If true, puts each attribute on a new line.

false

AttributeIndent

int

The number of additional indents beyond its original indent for an attribute.

2

XML serialization examples

The following example creates an element, then adds an element comment. Next, it demonstrates printing the element with the default settings that include comments and how to customize the output to omit comments.

uses gw.xml.XmlElement
uses javax.xml.namespace.QName
uses gw.xml.XmlSerializationOptions

// Create an element
var a = new XmlElement(new QName("http://mycompany.com/schema/vehiclexsd", "root", "veh"))

// Add a comment
a. $Comment = "Hello I am a comment"

print("print element with default settings...")
a.print()

print("print element with no comments...")
a.print(new XmlSerializationOptions() {: Comments = false})

For Guidewire application messaging, follow the pattern of the following PolicyCenter Event Fired rule code. It creates a new message that contains the XML for a contact entity as a String to work with the standard message payload property. The messaging system requires a String, not an array of bytes. To properly and safely encode XML into a String, use the syntax:

if (MessageContext.EventName == "ContactChanged") {
  var xml = new mycompany.messaging.ContactModel.Contact(MessageContext.Root as Contact)
  var strContent = gw.util.Base64Util.encode(xml.bytes())

  var msg = MessageContext.createMessage(strContent)

  print("Message payload of my changed contact for debugging:")
  print(msg)
}

Your messaging transport code takes the payload String and exports it:

override function send(message : Message, transformedPayload : String) : void  {

  // Decode the Base64 encoded bytes stored in a String.
  var bytes = Base64Util.decode(message.Payload)

  // Send the byte array to a foreign system.
  ...

  message.reportAck();
}

All serialization APIs generate XML data for the entire XML hierarchy with that element at the root.