Home > Class Reference > ENSLIB namespace > %XML.Writer


class %XML.Writer extends %Library.RegisteredObject

The %XML.Writer controls export of XML from Caché. It generates a complete XML document including the <?xml> tag. %XML.Writer works sequentially, outputting the XML as it goes.

%XML.Writer works in two phases. During the first phase, any required parameters are set for the entire document, for example indentation, charset, output media. In the second phase the root element and its child elements are exported. The root element may be the export of a single object and its referenced objects. Or the root element may be a simple element with any number of objects exported as its children.

All parts of the XML document should be output using methods of this class. The Write method may be used to output unformatted and unescaped text inside the the root element of the document. In this case the user is responsible for proper management of namespaces and nesting. Additionally, the Object() and Element() may be used to output nested elements.

Namespace handling is for the most part automatic. %XML.Writer adds namespaces, with automatically created prefixes, to the XML output as needed. The usage of namespaces is controlled by the ElementQualified, AttributeQualified and DefaultNamespace properties, and any namespace argument to an Object or Element call. However, it is possible to control when a namespace is introduced into the document and the prefix used with the AddNamespace() method.

The following usage example wraps two instances of the Sample.Person class in tags and sets the root tag to as an example of possible adherence to a business rule:

   set x1=##class(Sample.Person).%OpenId(1)
   set x2=##class(Sample.Person).%OpenId(2)
   set writer=##class(%XML.Writer).%New()
   set writer.Charset="UTF-8"
   set sc=writer.OutputToString()
   If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit
   s sc=writer.RootElement("CustomerList")
   If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit
   set sc=writer.Object(x1,"Customer")
   If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit
   set sc=writer.Object(x2,"Customer") 
   If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit
   s sc=writer.EndRootElement()
   If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit
   s string=writer.GetXMLString()

Property Inventory

Method Inventory


property AttributeQualified as %Boolean [ InitialExpression = 0 ];
AttributeQualified indicates whether or not locally declared attributes must be qualified. See attributeFormDefault schema attribute.
Property methods: AttributeQualifiedDisplayToLogical(), AttributeQualifiedGet(), AttributeQualifiedIsValid(), AttributeQualifiedLogicalToDisplay(), AttributeQualifiedNormalize(), AttributeQualifiedSet()
property Base64LineBreaks as %Boolean [ InitialExpression = 0 ];
If Base64LineBreaks is true (1), then remove line breaks from base64 encoded XML output for all properties of type %Binary or %xsd.base64Binary. The default is 0 which will be to not include line breaks.
Property methods: Base64LineBreaksDisplayToLogical(), Base64LineBreaksGet(), Base64LineBreaksIsValid(), Base64LineBreaksLogicalToDisplay(), Base64LineBreaksNormalize(), Base64LineBreaksSet()
property Charset as %String;
Charset is the charset to use for encoding the XML output. The default depends upon the output destination. "UTF-8" is the default for output to files and binary streams. On a Unicode Caché, "UTF-16 is the default for output to character streams and strings. On an 8 bit Caché, the default charset for the locale is the default charset for output to character streams and strings.
Property methods: CharsetDisplayToLogical(), CharsetGet(), CharsetIsValid(), CharsetLogicalToDisplay(), CharsetLogicalToOdbc(), CharsetNormalize(), CharsetSet()
property CurrentIndent as %String;
The current indent level.