Article
· Jan 26, 2022 2m read

How to apply DTL on XML String in Object Script

Requirement: Transform source XML message to target JSON.

step1: First create json equivalent xml from any online tool.

step2: use add in  studio utility create persistent classes from json equivalent xml and compile it , now target persistent classes are ready

step3: Do the above step for source xml or xsd and generate persistent classes for source and compile it

step4: complete the DTL by selecting root node from source, target persistent classes and complete the mappings and compile it

step5: use the below code to apply Transformation on source XML string and create Target JSON

Assume you received xml string and stored in rsXML variable.

//to remove enter char , other formatting chars form string

set rsXML$TR(rsXML,$c(13,10,9))


set reader=##class(%XML.Reader).%New()
set status=reader.OpenString(rsXML)
if $$$ISERR(status{$$$TRACE("sc Error: "_status)}

// Here rootTagName ="ReportMsg" and equivalent Persistent class name is "org.prashanth.demo.source.ReportMsg"

do reader.Correlate("ReportMsg","org.prashanth.demo.source.ReportMsg")
do reader.Next(.myobj,.status)
quit:$$$ISERR(statusstatus

$$$LOGINFO("source xml Object: "_myobj)

//Here DTL name is "org.prashanth.demo.dtl.DemoDTL" and DTL result will be available in .pResDTLXml
set tSC ##class(org.prashanth.demo.dtl.DemoDTL).Transform(myobj,.pResDTLXml)

// To Create JSON Stream form DTL result

set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.jsonMsg,pResDTLXml,,,1,"aelqtw")

//to log JSON message from Stream
$$$LOGINFO("JSON = " _jsonMsg.Read())

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