Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi XSD Databinding Wizard (https://www.delphipraxis.net/72534-xsd-databinding-wizard.html)

Jelly 2. Jul 2006 13:18


XSD Databinding Wizard
 
Ich beschäftige mich grad mit XSD Dateien und XML Dateien. Ich hab eine XSD Datei vorliegen. Mit dem XML Databinding Wizard wird mir z.B. folgende Unit erstellt:
Delphi-Quellcode:
{***************************************************************************************************************************}
{                                                                                                                           }
{                                                     XML Data Binding                                                     }
{                                                                                                                           }
{         Generated on: 02.07.2006 13:38:46                                                                                 }
{       Generated from: C:\Programme\Microsoft.NET\SDK\v1.1\QuickStart\howto\samples\xml\datasetmapxsdschema\cp\books.xsd  }
{   Settings stored in: C:\Programme\Microsoft.NET\SDK\v1.1\QuickStart\howto\samples\xml\datasetmapxsdschema\cp\books.xdb  }
{                                                                                                                           }
{***************************************************************************************************************************}

unit books;

interface

uses xmldom, XMLDoc, XMLIntf;

type

{ Forward Decls }

  IXMLBookstoreType = interface;
  IXMLBookType = interface;
  IXMLAuthorName = interface;

{ IXMLBookstoreType }

  IXMLBookstoreType = interface(IXMLNodeCollection)
    ['{0A282638-866F-433A-8D0D-56779ED9C48A}']
    { Property Accessors }
    function Get_Book(Index: Integer): IXMLBookType;
    { Methods & Properties }
    function Add: IXMLBookType;
    function Insert(const Index: Integer): IXMLBookType;
    property Book[Index: Integer]: IXMLBookType read Get_Book; default;
  end;

{ IXMLBookType }

  IXMLBookType = interface(IXMLNode)
    ['{7F1161D5-382F-49BF-88B7-CEEFC5931A50}']
    { Property Accessors }
    function Get_Genre: WideString;
    function Get_Title: WideString;
    function Get_Author: IXMLAuthorName;
    function Get_Price: WideString;
    procedure Set_Genre(Value: WideString);
    procedure Set_Title(Value: WideString);
    procedure Set_Price(Value: WideString);
    { Methods & Properties }
    property Genre: WideString read Get_Genre write Set_Genre;
    property Title: WideString read Get_Title write Set_Title;
    property Author: IXMLAuthorName read Get_Author;
    property Price: WideString read Get_Price write Set_Price;
  end;

{ IXMLAuthorName }

  IXMLAuthorName = interface(IXMLNode)
    ['{174B60BC-E05A-47E3-8AE9-C1ADBA99D409}']
    { Property Accessors }
    function Get_Firstname: WideString;
    function Get_Lastname: WideString;
    procedure Set_Firstname(Value: WideString);
    procedure Set_Lastname(Value: WideString);
    { Methods & Properties }
    property Firstname: WideString read Get_Firstname write Set_Firstname;
    property Lastname: WideString read Get_Lastname write Set_Lastname;
  end;

{ Forward Decls }

  TXMLBookstoreType = class;
  TXMLBookType = class;
  TXMLAuthorName = class;

{ TXMLBookstoreType }

  TXMLBookstoreType = class(TXMLNodeCollection, IXMLBookstoreType)
  protected
    { IXMLBookstoreType }
    function Get_Book(Index: Integer): IXMLBookType;
    function Add: IXMLBookType;
    function Insert(const Index: Integer): IXMLBookType;
  public
    procedure AfterConstruction; override;
  end;

{ TXMLBookType }

  TXMLBookType = class(TXMLNode, IXMLBookType)
  protected
    { IXMLBookType }
    function Get_Genre: WideString;
    function Get_Title: WideString;
    function Get_Author: IXMLAuthorName;
    function Get_Price: WideString;
    procedure Set_Genre(Value: WideString);
    procedure Set_Title(Value: WideString);
    procedure Set_Price(Value: WideString);
  public
    procedure AfterConstruction; override;
  end;

{ TXMLAuthorName }

  TXMLAuthorName = class(TXMLNode, IXMLAuthorName)
  protected
    { IXMLAuthorName }
    function Get_Firstname: WideString;
    function Get_Lastname: WideString;
    procedure Set_Firstname(Value: WideString);
    procedure Set_Lastname(Value: WideString);
  end;

{ Global Functions }

function Getbookstore(Doc: IXMLDocument): IXMLBookstoreType;
function Loadbookstore(const FileName: WideString): IXMLBookstoreType;
function Newbookstore: IXMLBookstoreType;

const
  TargetNamespace = '';

implementation

{ Global Functions }

function Getbookstore(Doc: IXMLDocument): IXMLBookstoreType;
begin
  Result := Doc.GetDocBinding('bookstore', TXMLBookstoreType, TargetNamespace) as IXMLBookstoreType;
end;

function Loadbookstore(const FileName: WideString): IXMLBookstoreType;
begin
  Result := LoadXMLDocument(FileName).GetDocBinding('bookstore', TXMLBookstoreType, TargetNamespace) as IXMLBookstoreType;
end;

function Newbookstore: IXMLBookstoreType;
begin
  Result := NewXMLDocument.GetDocBinding('bookstore', TXMLBookstoreType, TargetNamespace) as IXMLBookstoreType;
end;

{ TXMLBookstoreType }

procedure TXMLBookstoreType.AfterConstruction;
begin
  RegisterChildNode('book', TXMLBookType);
  ItemTag := 'book';
  ItemInterface := IXMLBookType;
  inherited;
end;

function TXMLBookstoreType.Get_Book(Index: Integer): IXMLBookType;
begin
  Result := List[Index] as IXMLBookType;
end;

function TXMLBookstoreType.Add: IXMLBookType;
begin
  Result := AddItem(-1) as IXMLBookType;
end;

function TXMLBookstoreType.Insert(const Index: Integer): IXMLBookType;
begin
  Result := AddItem(Index) as IXMLBookType;
end;

{ TXMLBookType }

procedure TXMLBookType.AfterConstruction;
begin
  RegisterChildNode('author', TXMLAuthorName);
  inherited;
end;

function TXMLBookType.Get_Genre: WideString;
begin
  Result := AttributeNodes['genre'].Text;
end;

procedure TXMLBookType.Set_Genre(Value: WideString);
begin
  SetAttribute('genre', Value);
end;

function TXMLBookType.Get_Title: WideString;
begin
  Result := ChildNodes['title'].Text;
end;

procedure TXMLBookType.Set_Title(Value: WideString);
begin
  ChildNodes['title'].NodeValue := Value;
end;

function TXMLBookType.Get_Author: IXMLAuthorName;
begin
  Result := ChildNodes['author'] as IXMLAuthorName;
end;

function TXMLBookType.Get_Price: WideString;
begin
  Result := ChildNodes['price'].Text;
end;

procedure TXMLBookType.Set_Price(Value: WideString);
begin
  ChildNodes['price'].NodeValue := Value;
end;

{ TXMLAuthorName }

function TXMLAuthorName.Get_Firstname: WideString;
begin
  Result := ChildNodes['first-name'].Text;
end;

procedure TXMLAuthorName.Set_Firstname(Value: WideString);
begin
  ChildNodes['first-name'].NodeValue := Value;
end;

function TXMLAuthorName.Get_Lastname: WideString;
begin
  Result := ChildNodes['last-name'].Text;
end;

procedure TXMLAuthorName.Set_Lastname(Value: WideString);
begin
  ChildNodes['last-name'].NodeValue := Value;
end;

end.
Sieht ja schonmal sehr vielversprechend aus, jedoch irgendwie krieg ich das nicht hin, damit zu Arbeiten, sprich neue Daten anzulegen und das Ganze in einer XML Datei abzuspeichern...

Folgende Code funktioniert ja, aber z.B. speichern krieg ich nicht hin:

Delphi-Quellcode:
     Store := Newbookstore ;
     Book := FStore.Add ;
     book.Title := 'Delphi Kochbuch' ;
     Book.Genre := 'Software' ;
     Book.Price := '49.45' ;
Mit dem Suchen innerhalb von unterschiedlichen "Books" hätt ich dann wahrscheinlich das nächste Problem.

marabu 5. Jul 2006 08:39

Re: XSD Databinding Wizard
 
Hallo Tom,

der Aufruf von NewBookstore() gibt dir einen Schnittstellenzeiger auf IXMLBookstoreType -> IXMLNodeCollection -> IXMLNode zurück. Das Speichern deines Dokuments sollte also so oder ähnlich funktionieren:

Delphi-Quellcode:
with Store as IXMLNode do
  ownerDocument.SaveToFile('jelly.xml');
Für das Durchsuchen verweise ich dich mal auf IDOMNodeSelect: klick

Grüße vom marabu

Jelly 5. Jul 2006 20:02

Re: XSD Databinding Wizard
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hey, servus,

ich konnte mich in den letzten Tagen bischen damit beschäftigen, und ich bin schlichtweg begeistert wie diese XML Anbindung gelöst wurde und wie einfach es damit wird, mit XML Dateien umzugehen...

Ich hab hier mal im Posting die wichtigsten Parts nochmal reingehängt. Die Methode öffnet die XML Datei, und fügt 2 neue Titel hinzu und speichert den ganzen Spass wieder ab.

Hier mein Code zum Ergänzen von Büchern:
Delphi-Quellcode:
procedure TForm12.cmdAppendClick(Sender: TObject);
var
  FStore: IXMLBookstoreType;
  FBook : IXMLBookType ;
  FCustomer : IXMLAuthorName ;
begin
     try
        XMLDocStore := TXMLDocument.Create(Self);
        XMLDocStore.FileName := BookStoreFile;   // variant 1
        FStore := Getbookstore(XMLDocStore); // variant 1
        XMLDocStore.Active := true;
        //FStore := Loadbookstore(BookStoreFile); // variant 2
     except
        on E:Exception do
           ShowMessage(Format('Error occurred loading: %s',[E.Message]));
     end;


     FBook := FStore.Add ;
     Fbook.Title := 'Delphi Kochbuch' ;
     FBook.Id := 1 ;
     FBook.Genre := 'Software' ;
     FBook.Price := 49.45 ;
     FBook.Author.Firstname := 'Thomas' ;
     FBook.Author.Lastname := 'Doberenz' ;

     FBook := FStore.Add ;
     FBook.ID := 2 ;
     Fbook.Title := 'RFID Handbuch' ;
     FBook.Genre := 'Elektronik' ;
     FBook.Price := 1290.656 ;

     FBook := FStore.Add ;
     Fbook.Title := 'Der Entwickler' ;
     FBook.Id := 3 ;
     FBook.Genre := 'Fachzeitschrift' ;
     FBook.Price := 10.43 ;
     FBook.Author.Firstname := 'Andreas' ;
     FBook.Author.Lastname := 'Kosch' ;

     XMLDocStore.SaveToFile ;
     XMLDocStore.Free ;
end;
Und vielleicht noch von Interesse das Anzeigen von Daten aus der XML Datei:
Delphi-Quellcode:
procedure TForm12.cmdShowClick(Sender: TObject);
var
  FStore: IXMLBookstoreType;
  FBook : IXMLBookType ;
  i: integer;
begin
     try
        XMLDocStore := TXMLDocument.Create(Self);
        //XMLDocStore.Active := true;
        FStore := Loadbookstore(BookStoreFile); // variant 2
     except
        on E:Exception do
           ShowMessage(Format('Error occurred loading: %s',[E.Message]));
     end;

     lstBooks.items.clear ;
     for i := 0 to FStore.Count - 1  do begin
         FBook := FStore.Book[i] ;
         lstBooks.items.add (format('%s (%.2n €)',[FBook.Title,FBook.Price])) ;
     end;


     XMLDocStore.Free ;
end;
Der Rest wird vom Databinding Wizard erstellt, also in books.pas.

Wie gesagt, ich war echt sehr angetan von dieser eleganten Methode, mit XML Dateien umzugehen und kann eigentlich nur jedem raten, sich ein bischen damit zu beschäftigen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:39 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz