Serialize object delphi 7

Hey,
I want to serialize an object to xml with delphi 7,  i searched but i didn't find a full solution, i think with HttpRio we can do it .
thank you in advance.
khali khaliAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
It is possible ... using OPToSoapDomConvert class. HttpRio use it internally...
You need to use TRemotable object as input into function. You don't specify which object you need to serialize...

uses XMLIntf, XMLDoc, ActiveX, MSXML, OPToSOAPDomConv, TypInfo,
  msxmldom, xmldom, ComObj;

function ObjToXML(const AObject: TRemotable; AutoIdent: Boolean): WideString;
var
  Xdoc: XMLIntf.IXMLDocument;
  Root: IXmlNode;
  RefId: WideString;
  Namespace, TypeName: WideString;
  IsScalar: Boolean;
  conv: TOPToSoapDomConvert;
begin
  RemClassRegistry.ClassToURI(AObject.ClassType, Namespace, TypeName, IsScalar);

  Xdoc := NewXMLDocument;
  if AutoIdent then  
    Xdoc.Options := [doNodeAutoIndent] //activate the auto indentation
  else
    Xdoc.Options := [];
  Root := Xdoc.Node; 

  conv := TOPToSoapDomConvert.Create(nil);
  try
    conv.Name := 'XMLConv1';
    conv.SetSubComponent(False);
    conv.Options :=
      [//soSendMultiRefObj,
      //soRootRefNodesToBody,
      //soTryAllSchema,
      soCacheMimeResponse,
      soUTF8EncodeXML,
      soSendUntyped,
      soDocument
      ]; //soUTF8InHeader

    AObject.ObjectToSOAP(Root, Root, conv, TypeName, Namespace,
      [ocoDontPrefixNode, {ocoDontSerializeProps,} ocoDontPutTypeAttr], RefId);

    Result := FormatXMLData(Xdoc.Node.XML);
  finally
    FreeAndNil(conv);
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khali khaliAuthor Commented:
Thank you Sinisa Vuk, it works.
0
khali khaliAuthor Commented:
An other question, this solution works with an object not complex,  but  if i have this structure  it doesn't work
 
TObjectTest = class (Tobject)
  private
    FAbsenceId : integer;
    Flibelle   : string;
  public
     constructor create; overload;
  published
    property AbsenceId : Integer read FAbsenceId write FAbsenceId;
    property Libelle  : string read Flibelle write Flibelle;
  end;
    
TObjectRemote = class (TRemotable)
  private
    FlistAbsenceList : TObjectList ;
  public
    constructor create; overload;

  published
    property listAbsenceList : TObjectList read FlistAbsenceList write FlistAbsenceList;
  end;

Open in new window


an exception arrise at AObject.ObjectToSOAP(Root, Root, conv, TypeName, Namespace,
      [ocoDontPrefixNode, {ocoDontSerializeProps,} ocoDontPutTypeAttr], RefId); un the function ObjToXml
how we can serialize the TObjectRemote in this case. knowing that  FlistAbsenceList : TObjectList ; contains a list of TObjectTest

thank you in advance.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Sinisa VukSoftware architectCommented:
This is because Delphi cannot serialize TObjectList that way.... It can dynamic arrays ... so must be:
type
  TObjectTest = class (TRemotable)
  private
    FAbsenceId : integer;
    Flibelle   : string;
  public
     constructor create; overload;
  published
    property AbsenceId : Integer read FAbsenceId write FAbsenceId;
    property Libelle  : string read Flibelle write Flibelle;
  end;

  TMyObjectList = array of TObjectTest;

  TObjectRemote = class (TRemotable)
  private
    FlistAbsenceList : TMyObjectList;
  public
    constructor create; overload;
  published
    property listAbsenceList : TMyObjectList read FlistAbsenceList write FlistAbsenceList;
  end;

Open in new window

0
khali khaliAuthor Commented:
Thank you Sinisa Vuk, it works well right now.
0
khali khaliAuthor Commented:
Also, to deserialize i used this code but it doesn't work, the xml file is correct and it is a result of the serialization above.
function XMLToObj(const AObject: TRemotable; AutoIdent: Boolean): WideString;
var
  Xdoc: XMLIntf.IXMLDocument;
  Root, RootParent: IXmlNode;
  RefId: WideString;
  Namespace, TypeName: WideString;
  IsScalar: Boolean;
  conv: TOPToSoapDomConvert;
begin
  RemClassRegistry.ClassToURI(AObject.ClassType, Namespace, TypeName, IsScalar);

  Xdoc := NewXMLDocument;
   //Xdoc.LoadXMLDocu;
   Xdoc.LoadFromFile('MyFile.xml');
  if AutoIdent then
    Xdoc.Options := [doNodeAutoIndent] //activate the auto indentation
  else
    Xdoc.Options := [];
  Root      := Xdoc.Node;
  RootParent := Xdoc.DocumentElement;

  conv := TOPToSoapDomConvert.Create(nil);
  try
    conv.Name := 'XMLConv1';
    conv.SetSubComponent(False);
    conv.Options :=
      [//soSendMultiRefObj,
      //soRootRefNodesToBody,
      //soTryAllSchema,
      soCacheMimeResponse,
      soUTF8EncodeXML,
      soSendUntyped,
      soDocument
      ]; //soUTF8InHeader

    AObject.SOAPToObject(RootParent, RootParent, conv);

    Result := FormatXMLData(Xdoc.Node.XML);

  finally
    FreeAndNil(conv);
  end;
end;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.