Question
· Nov 14, 2017

How to Manipulate XML to Formatted String

I am using studio 2015 version which does not have dynamic objects and I need to read xml and convert that to a json I have managed to come close to json string but with little difficulties .I need to be able to identify the root element and specify where to put braces between objects  anyone  with any idea is welcome he is my code so far

ClassMethod WriteNodes(myfile As %String = "C:\Testdata\data.xml")
{
  set status=##class(%XML.TextReader).ParseFile(myfile,.textreader)
  
    if $$$ISERR(status)
      {
	    do $System.Status.DisplayError(status) 
	    quit
	  }

       set ptemp=##class(%Stream.FileCharacter).%New()
       set ptemp.Filename="C:\Testdata\ree.txt"
      //check status
     //iterate through document, node by node
  
       Do ptemp.Write( "{")
       while textreader.Read()
        {
           If (textreader.seq=462)
	         {
		       Do ptemp.Write("}")
		       Do ptemp.Write("]")
		       Do ptemp.Write("}")
		     }
		     
		       If ((textreader.Path="/[1]/[2]/[0]")&&(textreader.NodeType'= "endelement"))
	             {
		          Do ptemp.Write("{")
		         }
		            If ((textreader.Path="/[1]/[2]/[0]")&&(textreader.NodeType= "endelement"))
	                   {
		                Do ptemp.Write("},")
		              }
        
                   If ((textreader.Name'="")&&(textreader.NodeType'= "endelement"))
                      {
                        Do ptemp.Write($CHAR(34)_textreader.Name_$CHAR(34)_$CHAR(58))
                      }
                      Else
                      {
	                   Do ptemp.Write("")
	                  }
           
                  If (textreader.Value'="")
                     {
                      Do ptemp.Write($CHAR(34)_textreader.Value_$CHAR(34)_"," )
                     }
               Do ptemp.WriteLine()
        }

       Do ptemp.%Save()
       
       quit
}
Discussion (12)2
Log in or sign up to continue

If you don't want / need the content as object and just want to convert XML2JSON
why wasting time and energy to re-invent the wheel an not just using any of the many downloadable tools
and call them over $ZF(-2)  and consume the result ?
Google gave my some thousand hits of tested solutions e.g. https://github.com/sinelaw/xml-to-json

I mean it's doable with Caché but file_in => file_out is not more than a nice exercise for training.
 

@Thembelani Mlalazi - when mentioning a DC member in a post it looks like you're copying and pasting the member name in a way that carries over a hyperlink. That hyperlink markup seems to prevent the DC software from recognizing the user. I recommend using the Preview button as a way of checking that the "mention" syntax is correct.

In any case it may be overkill to mention someone who has already contributed to the thread, since they typically get notified of updates unless they have deliberately unsubscribed.

Here's a sample for creating a proxy object and outputting it as JSON:

Set tProxy = ##class(%ZEN.proxyObject).%New()
Set tProxy.Property1 = "Some value 1"
Set tProxy.Property2 = "Some value 2"
Set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.tStream,.tProxy)
Write "Result (blank means no error):",$System.Status.GetErrorText(tSC),!
Write "JSON output:",!
Do tStream.OutputToDevice()

This produces the following output:

Result (blank means no error):
JSON output:
{
        "Property1":"Some value 1",
        "Property2":"Some value 2"
}

Hi there Marc,

Do you know whether is there anything to perform the opposite (convert an object into a xml string)?

E.g: 

Class BaseClass.Harvest Extends (%SerialObject, %XML.Adaptor) [ ProcedureBlock ]
{
Parameter ELEMENTQUALIFIED = 1;
...
Property Number As %String(MAXLEN = "", XMLNAME = "Number");
Property PatientNumber As %String(MAXLEN = "", XMLNAME = "PatientNumber");
...

Outcome would be:

<xml>
<Harvest>

   <Number></Number>
   <PatientNumber></PatientNumber>
...

Many thanks