Serialize to XML

Is there any example to serialize/deserialize a TROArray descendant to an XML file ?

Hello,

If you just need to write content of TROArray descedent in XML file you could simply write in file a usual in delphi:

var ROArray:TROIntArray;
begin
…
WriteLn(F, ROArray.ContentAsXml);
…
end;

Best regards

And to read back from xml ?

Hello,

Sorry for not full answer.

If XML of TROArray is get from ContentAsXml method, then for applying this XML back to the TROArray you should write some syntax parser and sequently add values of tags to the array.

So it is more convenient to use TROXMLSerializer.

var
ROArray:TROIntArray;
doc1,doc2:IXMLDocument;
lxml:TROXMLSerializer;
begin
…
// Serializing
doc1:=NewROXmlDocument;
doc1.New(ROArray.ClassName);
lxml := TROXMLSerializer.Create(pointer(doc1.DocumentNode));
try
lxml.Write(ROArray.ClassName, ROArray.ClassInfo, ROArray);
finally
lxml.Free;
end;
doc1.SaveToFile(‘array.xml’);
…
//Deserializing
doc2:=NewROXmlDocument;
doc2.New;
doc2.LoadFromFile(‘array.xml’);
lxml := TROXMLSerializer.Create(pointer(doc2.DocumentNode));
try
lxml.Read(ROArray.ClassName, ROArray.ClassInfo, ROArray);
finally
lxml.Free;
end;
…
end;

Best regards

1 Like

It works! Is there any option to make the generated XML more human-readable i.e. with newlines and indentation ?

xmlDoc.FormatXMLData(SomeXMLString)

Hello,

I’m trying to deserialize XML which I have serialized throug ContentAsXML. I am using the following code:

  DC := TMyRODataObject.Create;
  MemoryStream := TStringStream.Create(XMLDocumentAsString);
  try
    XMLDocument := NewROXmlDocument;
    XMLDocument.New;
    XMLDocument.LoadFromStream(MemoryStream);
    XMLSerializer := TROXMLSerializer.Create(Pointer(XMLDocument.DocumentNode));
    try
      XMLSerializer.Read(DC.ClassName, DC.ClassInfo, DC);
      Result := DC;
    finally
      XMLSerializer.Free;
    end;
  finally
    MemoryStream.Free;
  end;

My DC object is always nil. The object is a TROComplexType object. All properties are published and some of them are also TROCompleyType.

How do I the deserialization? Could you help me?

Thank you!
Best regards!

There is no need to create a TROXMLSerializer instance manually.
You can use the XmlToObject and ObjectToXml functions from uROXmlSerializer.pas.

XmlToObject returns a TROComplexType object which must be casted into the current target type.

Thanks! But it doesn’t work for me! :frowning: I’ve got an error “unknown class struct” :frowning: The XML which I am trying to convert comes from “.ContentAsXml”

I am using the following code:
TMyDataObject(XMLToObject(ContentAsXml));

I’d assume that ContentAsXml generates a different XML code (where the root node isn’t the original object type name)
Please run XmlToObject and ContentAsXml and compare the XML codes. I have no Delphi on my PC.

This example works fine, thanks for that.
I use it to save TROArray descendants in a database.
Is there also a JSON variant available? XML’s overhead is huge.

Kind regards,

Michel

Hi,

we support Variant value in JsonMessage.

this code:

procedure TClientForm.Button1Click(Sender: TObject);
var
  k: IROMessage;
  str: TROBinaryMemoryStream;
  larray: NewArray;
begin
  str:= TROBinaryMemoryStream.Create();
  larray:= NewArray.Create;
  lArray.Add('some string');
  lArray.Add(Now);
  lArray.Add(True);
  lArray.Add(3456);
  lArray.Add(1234.0987);
  lArray.Add(Null);


  k := Message as IROMessage;

  k.Initialize(nil,'','',mtResponse);
  try
    k.WriteArray('', larray);
    k.WriteToStream(str);
    Memo1.Lines.LoadFromStream(str);
  finally
    k := nil;
    str.Free;
    larray.Free;
  end;
end;

will generate

{“version”:“1.1”,“result”:[“some string”,“2020-02-19T11:35:09.932”,true,3456,1234.0987,null]}