• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 37
  • Last Modified:

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.
0
khali khali
Asked:
khali khali
  • 3
  • 2
1 Solution
 
Sinisa VukCommented:
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
 
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
 
Sinisa VukCommented:
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

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now