Written by

Question Lee Butcher · Dec 3, 2024

Project objects to XML - flatten a property collection

I'm attempting to build a fairly complicated object graph, with nested objects and collections, in order to create a FHIR bundle.

In its most basic form there is a <bundle></bundle> element that represents the root, and there can be a number of nested <entry></entry> child elements.

I'm attempting to represent this as such:

class Bundle (%RegisteredObject, %XML.Adaptor)
{
    Property Entries As List Of Entry;
}

The problem is when this is projected it nests each Entry object within an Entries container, which makes sense but I don't want this. Is there a way to "flatten" or ignore the container? I can't find anything in the docs relating to this, nor any parameter values that would achieve it.

The XML should look like this instead:

<Bundle><Entry>
        ...
    </Entry><Entry>
        ...
    </Entry><Entry>
        ...
    </Entry></Bundle>
Product version: HealthShare 2017.2

Comments

Julius Kavay · Dec 3, 2024
Class DC.Bundle Extends (%RegisteredObject, %XML.Adaptor)
{
Property Entries As list Of Entry(XMLNAME = "Entry", XMLPROJECTION = "ELEMENT");ClassMethod Test()
{
	s bnd=..%New()
	s ent=##class(Entry).%New(),ent.Name="John",ent.Age=40d bnd.Entries.Insert(ent) k ent
	s ent=##class(Entry).%New(),ent.Name="Paul",ent.Age=45d bnd.Entries.Insert(ent) k ent
	
	d bnd.XMLExportToString(.xx) q xx
}
}

Class DC.Entry Extends (%RegisteredObject, %XML.Adaptor)
{
Property Name As%String;Property Age As%Integer;
}

The test output is:


USER>set xml=##class(DC.Bundle).Test()

USER>write xml
<Bundle><Entry><Name>John</Name><Age>40</Age></Entry><Entry><Name>Paul</Name><Age>45</Age></Entry></Bundle>
USER>

USER>zzxs xml
<Bundle>
    <Entry>
        <Name>John</Name>
        <Age>40</Age>
    </Entry>
    <Entry>
        <Name>Paul</Name>
        <Age>45</Age>
    </Entry>
</Bundle>

USER>
0
Lee Butcher  Dec 4, 2024 to Julius Kavay

That's great thank you very much for your help

0
Enrico Parisi · Dec 3, 2024

You can, this way:

Property Entry As list Of Entry(XMLPROJECTION = "ELEMENT");

Note that the tag is the name of the list property, not the list item.

0
Lee Butcher  Dec 4, 2024 to Enrico Parisi

Perfect, thank you, that works. I overlooked this because the docs said "ELEMENT" is the default behaviour so I assumed this was also the case for collection properties.

0
Lee Butcher  Dec 4, 2024 to Benjamin De Boe

We're still using Healthshare 2017.2 unfortunately. Do you know if there is anything similar for our version?

0
Benjamin De Boe  Dec 4, 2024 to Lee Butcher

Unfortunately, no. 2017.2 is indeed rather vintage.

0