How to XML project a Boolean/Integer property as an empty element

Primary tabs

For each instance of an XML-enabled class I want to control whether a property is projected as an empty element or not projected at all. With string properties, I can set the value as $c(0) for an empty element, or an empty string for no projection. But it looks like this isn't consistent for properties with type %Boolean or %Integer. With booleans $c(0) gets projected as "false". With integers $c(0) stays as a zero-width space in the XML output (which gives me a SAX parser error if I try to read it).

Is there a way to do this that works for all datatype properties?

  • 0
  • 0
  • 167
  • 1
  • 1

Answers

Comments

Here's the code I'm using to test btw. If you uncomment the commented line it gives the SAX parser error.

Class XML.Sample2 Extends (%RegisteredObject, %XML.Adaptor)
{

Property StringEmpty As %String;

Property StringZerowidth As %String;

Property IntegerEmpty As %Integer;

Property IntegerZerowidth As %Integer;

Property BoolTrue As %Boolean;

Property BoolFalse As %Boolean;

Property BoolZerowidth As %Boolean;

ClassMethod Test() As %Status
{
    set sample = ..%New()
    set sample.StringEmpty = ""
    set sample.StringZerowidth = $c(0)
    set sample.IntegerEmpty = ""
    //set sample.IntegerZerowidth = $c(0)
    set sample.BoolTrue = 1
    set sample.BoolFalse = 0
    set sample.BoolZerowidth = $c(0)
    set writer = ##class(%XML.Writer).%New()
    $$$QuitOnError(writer.OutputToString())
    $$$QuitOnError(writer.RootElement("root"))
    $$$QuitOnError(writer.Object(sample,"sample"))
    $$$QuitOnError(writer.EndRootElement())
    set string = writer.GetXMLString()
    w !, string
    set reader = ##class(%XML.Reader).%New()
    $$$QuitOnError(reader.OpenString(string))
    do reader.Correlate("sample","XML.Sample2")
    do reader.Next(.object, .sc)
    $$$QuitOnError(sc)
    for prop = "StringEmpty","StringZerowidth","IntegerEmpty","IntegerZerowidth","BoolTrue","BoolFalse","BoolZerowidth" {
        write !, prop, ": ", $replace($property(object,prop),$c(0),"$c(0)")
    }
    return $$$OK
}

}