Question
· Jul 28, 2021

Can I programatically import a class from an XML file?

If I export a class definition to an XML file, is there a way to programmatically import it so that I could schedule a task to look in a given location for XML files once a day and import class definitions?

Product version: IRIS 2019.1
Discussion (3)1
Log in or sign up to continue

Yes, you can!

Access your XML file as a binary stream, then you can use the LoadStream command to load & compile the stream.

 STREAM=##class(%Stream.FileBinary).%New()
 STFILE=STREAM.LinkToFile("c:\where\is\your\file.xml")
 STREAM.Rewind()
 ; test loading the stream, doesn't actually create it yet. The last '1' parameter means test & report.
 $System.OBJ.LoadStream(STREAM,"ckfsbry/lock=0",.ERR,.LOADED,1)
 ; You can check the ERR variable to see if it errors out with no changes to the system. Assuming none, rewind & load it "for real"
 STREAM.Rewind()
 $System.OBJ.LoadStream(STREAM,"ckfsbry/lock=0",.ERR,.LOADED)

I haven't tested this in this manner, as I actually have the XML Base-64 encoded in a global... but hopefully this should get you started. PM me if you'd like to see my full code; it's too long to add here.

I'll leave the scheduling part as an exercise to the reader. :-)

Hope this helps!