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 |
|---|---|---|---|
|
|
|
If |
|
|
|
|
If Warning: For large XML objects with many nested
layers, sorting requires significant computer
resources.
|
|
|
|
|
If |
|
|
|
|
If GX models created in Studio cannot be invalid. As a result, they do not need to be validated when serialized to XML. |
|
|
|
|
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 |
|
|
|
If |
|
The following properties provide options that take effect only if the
Pretty property is true:
|
Serialization option |
Type |
Description |
Default value |
|---|---|---|---|
|
|
|
The |
Two spaces |
|
|
|
The line separator as a |
The new line character (ASCII 10). |
|
|
|
If |
|
|
|
|
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.
