Question
· 18 hr ago

Projecting objects as XML - prevent close tag?

I'm trying to project a series of objects and nested objects to XML, and I'm having difficulties getting the XML representation exactly the way I need it to be. Here's what I'm working with, nothing complicated:

Class XMLTest Extends (%RegisteredObject, %XML.Adaptor)
{
    Property Id As Id;
}

Class Id Extends (%RegisteredObject, %XML.Adaptor)
{
    Property Value As %String (XMLPROJECTION = "attribute");
}

By default it seem closing tags are used with all elements, as such:

<XMLTest>
    <Id Value="D1949EE1-7D03-4666-9548-D1A949E10327"></Id>
</XMLTest>

Whereas I need these elements represented with just the forward slash terminator:

<XMLTest>
    <Id Value="D1949EE1-7D03-4666-9548-D1A949E10327"/>
</XMLTest>

Is there a way to configure the XML writer to achieve this, or a property declaration I can use?

Another question I have is around the structure of the classes that are to be projected, rather than the above main snippet, is it possible to use XML projection declarations to define that a simple property is an element with attributes and values? This would be a lot more convenient than creating classes just to hold their attribute values. For example something like:

Class XMLTest Extends (%RegisteredObject, %XML.Adaptor)
{
    Property Id As %String (XMLPROJECTION = "element", XMLATTRIBUTE = "value")
}

Any help is much appreciated.

Thanks

Product version: HealthShare 2017.2
$ZV: Cache for Windows (x86-64) 2017.2 (Build 744U) Fri Sep 29 2017 10:58:27 EDT
Discussion (2)3
Log in or sign up to continue

According to the documentation, it's not possible exporting an object.

Edit: however, as pointed out by @Herman Slagman in the next post, it's indeed possible, I'm going to provide feedback to documentation team.

You can manually create the XML document using %XML.Writer, like:

	set writer=##class(%XML.Writer).%New()
	set writer.Indent=1
	set writer.NoXMLDeclaration=1
	set status=writer.OutputToDevice()
	set status=writer.StartDocument()
	set status=writer.RootElement("XMLTest")
	set status=writer.Element("Id")	
	set status=writer.WriteAttribute("Attribute","D1949EE1-7D03-4666-9548-D1A949E10327")
	set status=writer.EndElement()
	set status=writer.EndRootElement()
	set status=writer.EndDocument()

The output of the above code is:

<XMLTest>
  <Id Attribute="D1949EE1-7D03-4666-9548-D1A949E10327"/>
</XMLTest>

Having said that, what's your issue with the closing tag? Semantically there is no difference, any system should be able to handle both cases (with or without closing tag).