Using TXMLDocument to serialize form settings to XML and database.

Posted by LukLed on Stack Overflow See other posts from Stack Overflow or by LukLed
Published on 2010-04-21T12:41:54Z Indexed on 2010/04/21 12:43 UTC
Read the original article Hit count: 200

I have an interface:

type IXMLSerializable = interface
  function SaveToXML : DOMString;
  function SaveToXMLDocument : IXMLDocument;
  procedure LoadFromXML(AXML : DOMString);
end;

It is used to serialize some settings of forms or frames to xml.

Simple implementation:

SaveToXMLDocument:

function TSomething.SaveToXMLDocument: IXMLDocument;
begin
  Result := TXMLDocument.Create(nil);

  with Result do
  begin
    Active := True;
    with AddChild(Self.Name) do
    begin
      AddChild(edSomeTextBox.Name).Attributes['Text'] := edSomeTextBox.Text;
    end;
  end;
  Result := XMLDoc;
end;

LoadFromXML:

procedure TSomething.LoadFromXML(AXML: DOMString);
var
  XMLDoc : IXMLDocument;
  I : Integer;
begin
  XMLDoc := TXMLDocument.Create(nil);

  with XMLDoc do
  begin
    LoadFromXML(AXML);
    Active := True;
    with ChildNodes[0] do
    begin
      for I := 0 to ChildNodes.Count-1 do
      begin
        If ChildNodes[I].NodeName = 'edSomeTextBox' then
          edSomeTextBox.Text := ChildNodes[I].Attributes['Text'];
      end;
    end;
  end;
end;

SaveToXML:

function TSomething.SaveToXML: DOMString;
begin
  SaveToXMLDocument.SaveToXML(Result);
end;

DOMString result of SaveToXML is saved to database to blob field. I had some encoding issues with other implementations and this one works fine (right now). Do you see any dangers in this code? Can I have issues with different settings on various machines and systems?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about Xml