· 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 ?")
SAMPLES>write dataset.Execute("b")
SAMPLES>do dataset.XMLExport()
<s:schema id="DefaultDataSet" xmlns="" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:s="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <s:element name="DefaultDataSet" msdata:IsDataSet="true">
      <s:choice maxOccurs="unbounded">
        <s:element name="SQL">
              <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:unique name="Constraint1" msdata:PrimaryKey="true">
      <s:selector xpath=".//SQL" />
      <s:field xpath="ID" />
<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">
    <Name>Bush,Sam K.</Name>
  <SQL diffgr:id="SQL2" msdata:rowOrder="1">
    <Name>Basile,Rhonda B.</Name>
  <SQL diffgr:id="SQL3" msdata:rowOrder="2">
    <Name>Bachman,Julie M.</Name>
  <SQL diffgr:id="SQL4" msdata:rowOrder="3">
    <Name>Bach,Mo N.</Name>
  <SQL diffgr:id="SQL5" msdata:rowOrder="4">
    <Name>Braam,William V.</Name>