Het zou een optie kunnen zijn om de xml databinder te gebruiken. (Als je even googled op xml databinder delphi, kom je redelijk wat voorbeelden tegen). Wat de databinder doet, is je xml of de definitie daarvan omzetten in een klasse. Hierdoor wordt het allemaal wat makkelijker. Zo ziet de XML data bind er uit van jou XML (heb alleen meerdere category items toegevoegd zodat de bind wizard weet dat het om meerdere items gaat).
Jou aangepaste XML:
Code:
<?xml version="1.0" standalone="yes"?>
<XMLConfig Application="POItool">
<Config TempFolder="Temp" TemplateFolder="Template" InputFolder="Input" OutputFolder="Output"/>
<Categories IconFolder="Icons">
<Category Name="Speedcam Fixed" Icon="speedcam.png" WarningSound="Yes" WarningDistance="150000"/>
<Category Name="Speedcam Fixed" Icon="speedcam.png" WarningSound="Yes" WarningDistance="150000"/>
</Categories>
</XMLConfig>
De door delphi gemaakte XML databind unit:
Code:
{***********************************************}
{ }
{ XML Data Binding }
{ }
{ Generated on: 29-7-2018 14:03:30 }
{ Generated from: E:\Documenten\nld.xml }
{ Settings stored in: E:\Documenten\nld.xdb }
{ }
{***********************************************}
unit nld;
interface
uses Xml.xmldom, Xml.XMLDoc, Xml.XMLIntf;
type
{ Forward Decls }
IXMLXMLConfigType = interface;
IXMLConfigType = interface;
IXMLCategoriesType = interface;
IXMLCategoryType = interface;
{ IXMLXMLConfigType }
IXMLXMLConfigType = interface(IXMLNode)
['{BA392240-6E2B-4744-A214-13DF39E9D583}']
{ Property Accessors }
function Get_Application: UnicodeString;
function Get_Config: IXMLConfigType;
function Get_Categories: IXMLCategoriesType;
procedure Set_Application(Value: UnicodeString);
{ Methods & Properties }
property Application: UnicodeString read Get_Application write Set_Application;
property Config: IXMLConfigType read Get_Config;
property Categories: IXMLCategoriesType read Get_Categories;
end;
{ IXMLConfigType }
IXMLConfigType = interface(IXMLNode)
['{E5A90EC3-428E-4632-A8BD-6BBB15687EB2}']
{ Property Accessors }
function Get_TempFolder: UnicodeString;
function Get_TemplateFolder: UnicodeString;
function Get_InputFolder: UnicodeString;
function Get_OutputFolder: UnicodeString;
procedure Set_TempFolder(Value: UnicodeString);
procedure Set_TemplateFolder(Value: UnicodeString);
procedure Set_InputFolder(Value: UnicodeString);
procedure Set_OutputFolder(Value: UnicodeString);
{ Methods & Properties }
property TempFolder: UnicodeString read Get_TempFolder write Set_TempFolder;
property TemplateFolder: UnicodeString read Get_TemplateFolder write Set_TemplateFolder;
property InputFolder: UnicodeString read Get_InputFolder write Set_InputFolder;
property OutputFolder: UnicodeString read Get_OutputFolder write Set_OutputFolder;
end;
{ IXMLCategoriesType }
IXMLCategoriesType = interface(IXMLNodeCollection)
['{CFD0DE3A-2A18-4C72-8630-EFC1953675B9}']
{ Property Accessors }
function Get_IconFolder: UnicodeString;
function Get_Category(Index: Integer): IXMLCategoryType;
procedure Set_IconFolder(Value: UnicodeString);
{ Methods & Properties }
function Add: IXMLCategoryType;
function Insert(const Index: Integer): IXMLCategoryType;
property IconFolder: UnicodeString read Get_IconFolder write Set_IconFolder;
property Category[Index: Integer]: IXMLCategoryType read Get_Category; default;
end;
{ IXMLCategoryType }
IXMLCategoryType = interface(IXMLNode)
['{E3F37563-770C-44DA-ACE2-DFF6566F18FF}']
{ Property Accessors }
function Get_Name: UnicodeString;
function Get_Icon: UnicodeString;
function Get_WarningSound: UnicodeString;
function Get_WarningDistance: Integer;
procedure Set_Name(Value: UnicodeString);
procedure Set_Icon(Value: UnicodeString);
procedure Set_WarningSound(Value: UnicodeString);
procedure Set_WarningDistance(Value: Integer);
{ Methods & Properties }
property Name: UnicodeString read Get_Name write Set_Name;
property Icon: UnicodeString read Get_Icon write Set_Icon;
property WarningSound: UnicodeString read Get_WarningSound write Set_WarningSound;
property WarningDistance: Integer read Get_WarningDistance write Set_WarningDistance;
end;
{ Forward Decls }
TXMLXMLConfigType = class;
TXMLConfigType = class;
TXMLCategoriesType = class;
TXMLCategoryType = class;
{ TXMLXMLConfigType }
TXMLXMLConfigType = class(TXMLNode, IXMLXMLConfigType)
protected
{ IXMLXMLConfigType }
function Get_Application: UnicodeString;
function Get_Config: IXMLConfigType;
function Get_Categories: IXMLCategoriesType;
procedure Set_Application(Value: UnicodeString);
public
procedure AfterConstruction; override;
end;
{ TXMLConfigType }
TXMLConfigType = class(TXMLNode, IXMLConfigType)
protected
{ IXMLConfigType }
function Get_TempFolder: UnicodeString;
function Get_TemplateFolder: UnicodeString;
function Get_InputFolder: UnicodeString;
function Get_OutputFolder: UnicodeString;
procedure Set_TempFolder(Value: UnicodeString);
procedure Set_TemplateFolder(Value: UnicodeString);
procedure Set_InputFolder(Value: UnicodeString);
procedure Set_OutputFolder(Value: UnicodeString);
end;
{ TXMLCategoriesType }
TXMLCategoriesType = class(TXMLNodeCollection, IXMLCategoriesType)
protected
{ IXMLCategoriesType }
function Get_IconFolder: UnicodeString;
function Get_Category(Index: Integer): IXMLCategoryType;
procedure Set_IconFolder(Value: UnicodeString);
function Add: IXMLCategoryType;
function Insert(const Index: Integer): IXMLCategoryType;
public
procedure AfterConstruction; override;
end;
{ TXMLCategoryType }
TXMLCategoryType = class(TXMLNode, IXMLCategoryType)
protected
{ IXMLCategoryType }
function Get_Name: UnicodeString;
function Get_Icon: UnicodeString;
function Get_WarningSound: UnicodeString;
function Get_WarningDistance: Integer;
procedure Set_Name(Value: UnicodeString);
procedure Set_Icon(Value: UnicodeString);
procedure Set_WarningSound(Value: UnicodeString);
procedure Set_WarningDistance(Value: Integer);
end;
{ Global Functions }
function GetXMLConfig(Doc: IXMLDocument): IXMLXMLConfigType;
function LoadXMLConfig(const FileName: string): IXMLXMLConfigType;
function NewXMLConfig: IXMLXMLConfigType;
const
TargetNamespace = '';
implementation
uses Xml.xmlutil;
{ Global Functions }
function GetXMLConfig(Doc: IXMLDocument): IXMLXMLConfigType;
begin
Result := Doc.GetDocBinding('XMLConfig', TXMLXMLConfigType, TargetNamespace) as IXMLXMLConfigType;
end;
function LoadXMLConfig(const FileName: string): IXMLXMLConfigType;
begin
Result := LoadXMLDocument(FileName).GetDocBinding('XMLConfig', TXMLXMLConfigType, TargetNamespace) as IXMLXMLConfigType;
end;
function NewXMLConfig: IXMLXMLConfigType;
begin
Result := NewXMLDocument.GetDocBinding('XMLConfig', TXMLXMLConfigType, TargetNamespace) as IXMLXMLConfigType;
end;
{ TXMLXMLConfigType }
procedure TXMLXMLConfigType.AfterConstruction;
begin
RegisterChildNode('Config', TXMLConfigType);
RegisterChildNode('Categories', TXMLCategoriesType);
inherited;
end;
function TXMLXMLConfigType.Get_Application: UnicodeString;
begin
Result := AttributeNodes['Application'].Text;
end;
procedure TXMLXMLConfigType.Set_Application(Value: UnicodeString);
begin
SetAttribute('Application', Value);
end;
function TXMLXMLConfigType.Get_Config: IXMLConfigType;
begin
Result := ChildNodes['Config'] as IXMLConfigType;
end;
function TXMLXMLConfigType.Get_Categories: IXMLCategoriesType;
begin
Result := ChildNodes['Categories'] as IXMLCategoriesType;
end;
{ TXMLConfigType }
function TXMLConfigType.Get_TempFolder: UnicodeString;
begin
Result := AttributeNodes['TempFolder'].Text;
end;
procedure TXMLConfigType.Set_TempFolder(Value: UnicodeString);
begin
SetAttribute('TempFolder', Value);
end;
function TXMLConfigType.Get_TemplateFolder: UnicodeString;
begin
Result := AttributeNodes['TemplateFolder'].Text;
end;
procedure TXMLConfigType.Set_TemplateFolder(Value: UnicodeString);
begin
SetAttribute('TemplateFolder', Value);
end;
function TXMLConfigType.Get_InputFolder: UnicodeString;
begin
Result := AttributeNodes['InputFolder'].Text;
end;
procedure TXMLConfigType.Set_InputFolder(Value: UnicodeString);
begin
SetAttribute('InputFolder', Value);
end;
function TXMLConfigType.Get_OutputFolder: UnicodeString;
begin
Result := AttributeNodes['OutputFolder'].Text;
end;
procedure TXMLConfigType.Set_OutputFolder(Value: UnicodeString);
begin
SetAttribute('OutputFolder', Value);
end;
{ TXMLCategoriesType }
procedure TXMLCategoriesType.AfterConstruction;
begin
RegisterChildNode('Category', TXMLCategoryType);
ItemTag := 'Category';
ItemInterface := IXMLCategoryType;
inherited;
end;
function TXMLCategoriesType.Get_IconFolder: UnicodeString;
begin
Result := AttributeNodes['IconFolder'].Text;
end;
procedure TXMLCategoriesType.Set_IconFolder(Value: UnicodeString);
begin
SetAttribute('IconFolder', Value);
end;
function TXMLCategoriesType.Get_Category(Index: Integer): IXMLCategoryType;
begin
Result := List[Index] as IXMLCategoryType;
end;
function TXMLCategoriesType.Add: IXMLCategoryType;
begin
Result := AddItem(-1) as IXMLCategoryType;
end;
function TXMLCategoriesType.Insert(const Index: Integer): IXMLCategoryType;
begin
Result := AddItem(Index) as IXMLCategoryType;
end;
{ TXMLCategoryType }
function TXMLCategoryType.Get_Name: UnicodeString;
begin
Result := AttributeNodes['Name'].Text;
end;
procedure TXMLCategoryType.Set_Name(Value: UnicodeString);
begin
SetAttribute('Name', Value);
end;
function TXMLCategoryType.Get_Icon: UnicodeString;
begin
Result := AttributeNodes['Icon'].Text;
end;
procedure TXMLCategoryType.Set_Icon(Value: UnicodeString);
begin
SetAttribute('Icon', Value);
end;
function TXMLCategoryType.Get_WarningSound: UnicodeString;
begin
Result := AttributeNodes['WarningSound'].Text;
end;
procedure TXMLCategoryType.Set_WarningSound(Value: UnicodeString);
begin
SetAttribute('WarningSound', Value);
end;
function TXMLCategoryType.Get_WarningDistance: Integer;
begin
Result := AttributeNodes['WarningDistance'].NodeValue;
end;
procedure TXMLCategoryType.Set_WarningDistance(Value: Integer);
begin
SetAttribute('WarningDistance', Value);
end;
end.
En vervolgens hoe je het kunt gebruiken. ik heb niet alle properties gebruikt, maar daar kom je wel uit denk ik.
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
XMLConfig: IXMLXMLConfigType;
Config: IXMLConfigType;
Categories: IXMLCategoriesType;
Category: IXMLCategoryType;
I: Integer;
begin
if OpenDialog1.Execute then
begin
XMLDocument1.LoadFromFile(OpenDialog1.FileName);
XMLDocument1.Active := True;
XMLConfig := GetXMLConfig(XMLDocument1);
Config := XMLConfig.Config;
Memo1.Lines.Add('Temp folder: ' + Config.TempFolder);
Memo1.Lines.Add('Input folder: ' + Config.InputFolder);
Categories := XMLConfig.Categories;
for I := 0 to Categories.Count - 1 do
begin
Category := Categories.Category[I];
Memo1.Lines.Add('Name: ' + Category.Name);
Memo1.Lines.Add('Warning Distance: ' + IntToStr(Category.WarningDistance));
end;
end;
end;
Bookmarks