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
}
But 2015 and before has
class %ZEN.Auxiliary.jsonProvider
http://docs.intersystems.com/cache201513/csp/documatic/%25CSP.Documatic....
@Robert.Cemper thank you but I am trying to avoid using the object classes to create this as I will be processing a lot of different files and will end up with a lot of classes
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.
that works wonders with the use of the following class http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GXML_xslt
%ZEN.Auxiliary.jsonProvider can make use of %ZEN.proxyObject to represent the JSON. This wouldn't require a formal class definition for your objects.
Thanks, totally correct!
It's mentioned in line 5 of the doc I pointed to.
@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:
This produces the following output:
Hi there Marc,
Do you know whether is there anything to perform the opposite (convert an object into a xml string)?
E.g:
{
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
Sure. You're already extending %XML.Adaptor, which includes the methods XMLExportToStream and XMLExportToString.
@Marc Mundt I have read around the topic but still cannot figure out how to read an xml to %ZEN.proxyObject would mind please giving us an example to work with I am still new to this thank you
How about an XML Stylesheet?
https://stackoverflow.com/questions/24122921/xsl-to-convert-xml-to-json
https://stackoverflow.com/questions/43355563/convert-xml-to-json-using-xslt