我想学习xml的使用方法,想着个例子研究研究,谢谢
解决方案 »
- 如何让程序实现 安全删除 u盘 <或> 让在U盘里运行的程序不影响U盘的安全删除????
- 当整个应用程序失去焦点触发什么消息?
- 欢迎加入Delphi群:45755097 互相交流,共同进步
- 求一个TFileStream拷贝文件的实例
- QuickReport报表在XP下怎么不能用呢???
- 关于delphi调用dll的困惑
- 如何删除多条记录!!!
- 如何在LISTBOX中显示SQL表内一个字段'MC'
- Delphi中如何编写关闭功能,为什么点击关闭,总出错?其它功能正常,就是关的时候出现错误
- 取得局域网中另外一台计算机的时间??
- 请问怎样让TryStrToDatetime('1999年1月1日',D)返回True?
- 请达人介绍一下,有关工程硕士的研究方向选择,刚入学,很迷茫...................
var
xmlRoot: IXMLNode;
xmlAdres: IXMLNode;
xmlAdresItem: IXMLNode;
begin
XMLDocument.FileName := '.\XMLDocument1.xml';
XMLDocument.Active := True; try
xmlRoot := XMLDocument.DocumentElement;
xmlAdres := xmlRoot.ChildNodes.FindNode('adres'); while Assigned(xmlAdres) do
begin
xmlAdresItem := xmlAdres.ChildNodes[0]; while Assigned(xmlAdresItem) do
begin
Memo.Lines.Add(xmlAdresItem.Text);
xmlAdresItem := xmlAdresItem.NextSibling;
end; Memo.Lines.Add('--------');
xmlAdres := xmlAdres.NextSibling;
end;
finally
XMLDocument.Active := False;
end;
end;
Author: Benjamin Heil
0 Comments to this tip [Write new comment]
[ Print tip ] Tip Rating (13):
{
The following procedure allows you to build a menu from an XML file.
Special feature: You only need to specify the Name of the procedure which then
will be attached to a OnClick handler.
Note that the procedure must be declared as public.
} {
Mit folgender Prozedur kann man aus einem XML-File ein Menu
erstellen lassen (einfach im OnCreate aufrufen).
Besonderes Feature: Im XML-File gebt ihr nur den Namen der Prozedur an,
die dem OnClick-Ereignis zugewiesen werden soll.
Die einzige Einschränkung besteht darin, dass diese Prozedur
published sein muss.
Bindet einfach diese Prozedur in euer Hauptformular ein:
}
procedure TMainForm.CreateMenuFromXMLFile; function Get_Int(S: string): Integer;
begin
Result := 0;
try
Result := StrToInt(S);
except
end;
end; procedure AddRecursive(Parent: TMenuItem; Item: IXMLNode);
var
I: Integer;
Node: TMenuItem;
Child: IXMLNode;
Address: TMethod;
begin
Node := TMenuItem.Create(Parent);
if (Uppercase(Item.Attributes['CAPTION']) <> 'SEPERATOR') then
begin
Node.Caption := Item.Attributes['CAPTION'];
if (Uppercase(Item.Attributes['ID']) <> 'NONE') then
begin
Address.Code := MethodAddress(Item.Attributes['ID']);
Address.Data := Self;
if (Item.ChildNodes.Count - 1 < 0) then
Node.OnClick := TNotifyEvent(Address);
end;
if (Uppercase(Item.Attributes['SHORTCUT']) <> 'NONE') then
Node.ShortCut := TextToShortCut(Item.Attributes['SHORTCUT']);
Node.Checked := (Item.Attributes['CHECKED'] = '1');
end
else
Node.Caption := '-';
Node.Visible := (Item.Attributes['VISIBLE'] = '1'); if Parent <> nil then
Parent.Add(Node)
else
MainMenu.Items.Add(Node); for I := 0 to Item.ChildNodes.Count - 1 do
begin
Child := item.ChildNodes[i];
if (Child.NodeName = 'ENTRY') then
AddRecursive(Node, Child);
end;
end;
var
Root: IXMLMENUType;
Parent: TMenuItem;
I: Integer;
Child: IXMLNode;
begin
XMLDocument.FileName := ExtractFilePath(Application.ExeName) + XMLFile;
if not FileExists(XMLDocument.FileName) then
begin
MessageDlg('Menu-XML-Document nicht gefunden!', mtError, [mbOK], 0);
Halt;
end;
XMLDocument.Active := True; Screen.Cursor := crHourglass;
try
Root := GetXMLMenu(XMLDocument);
Parent := nil; for I := 0 to Root.ChildNodes.Count - 1 do
begin
Child := Root.ChildNodes[i];
if (Child.NodeName = 'ENTRY') then
AddRecursive(Parent, Child);
end;
finally
Screen.Cursor := crDefault;
end;
end;