Question
· Mar 18, 2016

turn-key export of data for XML-enabled class?

An XML-enabled class has an XMLExport() method that knows how to serialize an instance to XML. To export all instances of a class to a file, I need to instantiate a writer and loop through the extent. Is there no class method like XMLExportToFile() that does this in one go?

Discussion (3)1
Log in or sign up to continue

Since XMLEnabled class can be not persistent and, therefore, not have an extent at all, there is no such method. If you need to write  XMLExportToFile()  for several classes you can write method generator which generates a method that would:

  1. Use current extent query
  2. Iterate through it
  3. Output each object into a file

And inherit all classes, in which you have a need of XMLExportToFile()  method from this class.

Hi Jon,

I'm not sure this is exactly what you were looking for but you should be aware of the %XML.DataSet class.

With this class you can run an SQL query (for example returning all the instances of your class, or filter out some subset of it) and get the results in XML format (including the metadata of the result).

See more details in the docs.

Here's a sample run:

 
SAMPLES>set dataset=##class(%XML.DataSet).%New()
 
SAMPLES>write dataset.Prepare("SELECT TOP 5 ID, Name, DOB FROM Sample.Person WHERE Name %STARTSWITH ?")
1
SAMPLES>write dataset.Execute("b")
1
SAMPLES>do dataset.XMLExport()
<SQLResult>
<s:schema id="DefaultDataSet" xmlns="" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <s:element name="DefaultDataSet" msdata:IsDataSet="true">
    <s:complexType>
      <s:choice maxOccurs="unbounded">
        <s:element name="SQL">
          <s:complexType>
            <s:sequence>
              <s:element name="ID" type="s:long" minOccurs="0" />
              <s:element name="Name" type="s:string" minOccurs="0" />
              <s:element name="DOB" type="s:date" minOccurs="0" />
            </s:sequence>
          </s:complexType>
        </s:element>
      </s:choice>
    </s:complexType>
    <s:unique name="Constraint1" msdata:PrimaryKey="true">
      <s:selector xpath=".//SQL" />
      <s:field xpath="ID" />
    </s:unique>
  </s:element>
</s:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DefaultDataSet xmlns="">
  <SQL diffgr:id="SQL1" msdata:rowOrder="0">
    <ID>3</ID>
    <Name>Bush,Sam K.</Name>
    <DOB>1932-05-05</DOB>
  </SQL>
  <SQL diffgr:id="SQL2" msdata:rowOrder="1">
    <ID>53</ID>
    <Name>Basile,Rhonda B.</Name>
    <DOB>2001-07-18</DOB>
  </SQL>
  <SQL diffgr:id="SQL3" msdata:rowOrder="2">
    <ID>66</ID>
    <Name>Bachman,Julie M.</Name>
    <DOB>1959-10-11</DOB>
  </SQL>
  <SQL diffgr:id="SQL4" msdata:rowOrder="3">
    <ID>102</ID>
    <Name>Bach,Mo N.</Name>
    <DOB>1936-01-17</DOB>
  </SQL>
  <SQL diffgr:id="SQL5" msdata:rowOrder="4">
    <ID>116</ID>
    <Name>Braam,William V.</Name>
    <DOB>1983-03-06</DOB>
  </SQL>
</DefaultDataSet>
</diffgr:diffgram>
</SQLResult>