Thema: Delphi XSD Databinding Wizard

Einzelnen Beitrag anzeigen

Benutzerbild von Jelly
Jelly

Registriert seit: 11. Apr 2003
Ort: Moestroff (Luxemburg)
3.741 Beiträge
 
Delphi 2007 Professional
 
#1

XSD Databinding Wizard

  Alt 2. Jul 2006, 14:18
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.
  Mit Zitat antworten Zitat