Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期四, 10月 04, 2007

XML TOOL


首先新增
3個TLabel;
3個TEdit;
1個TTreeView;
1個TRichEdit; 屬性ScrollBars=ssBoth
1個TOpenDialog; 屬性Filter=XML File (*.xml)|*.xml|Any File (*.*)|*.*; 屬性Title=Load XML File
1個TSaveDialog; 屬性Filter=XML File (*.xml)|*.xml|Any File (*.*)|*.*; 屬性Title=Save XML File
2個TBitBtn; 屬性Glyph加入自己的圖示;

程式須要 XMLTBase.pas 及 XMLTool.pas

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, XMLTool, XMLIntf, XMLDoc, Clipbrd, Buttons;

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
TreeView1: TTreeView;
RichEdit1: TRichEdit;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure TreeView1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
FXmlTool: TXMLTool;
{ Private declarations }
public
procedure RefreshViewer;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.RefreshViewer;
procedure DrawTree(TreeNode: TTreeNode; XPath: string);
var
SubNode: TTreeNode;
begin
if (XPath <> '') and (XPath[Length(XPath)] <> '/') then
XPath := XPath + '/';

repeat
if FXmlTool.SubNodes[XPath].NodeIsText then
begin
SubNode := TreeView1.Items.AddChildObject(
TreeNode,
FXmlTool.SubNodes[XPath].NodeName + ' = ''' + FXmlTool.SubNodes[XPath].NodeText + '''',
Pointer(FXmlTool.SubNodes[XPath].Node)
);
end else begin
SubNode := TreeView1.Items.AddChildObject(
TreeNode,
FXmlTool.SubNodes[XPath].NodeName,
Pointer(FXmlTool.SubNodes[XPath].Node)
);
end;
if FXmlTool.SubNodes[XPath].NodeHasChild or FXmlTool.SubNodes[XPath].NodeHasAttr then
DrawTree(SubNode, FXmlTool.SubNodes[XPath].NodeXPath);
until not FXmlTool.SubNodes[XPath].Next;

if XPath = Edit2.Text + '/' then
begin
TreeView1.Selected := TreeNode;
TreeNode.Expanded := True;
end;
end;
begin
TreeView1.Items.Clear;
DrawTree(TreeView1.Items.AddChildObject(nil, '/', Pointer(FXmlTool.Node['/'])), '/');
TreeView1.Items[0].Expanded := True; // 第一層展開的
if (TreeView1.Selected = nil) and (TreeView1.Items.Count > 0) then
TreeView1.Selected := TreeView1.Items[0];
TreeView1Click(nil);
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
if TreeView1.Selected <> nil then
begin
Edit2.Text := FXMLTool.NodeToXPath(IXMLNode(TreeView1.Selected.Data)); //顯示XML PATH
Edit3.Text := FXmlTool[Edit2.Text]; //顯示選擇標籤文字
RichEdit1.Text := FXmlTool.NodeXML[Edit2.Text]; //顯示路徑下的所有內容
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FXmlTool := TXMLTool.Create;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
FXmlTool.LoadFromFile(OpenDialog1.FileName);
Edit1.Text := OpenDialog1.FileName; //Edit1文字為檔案路徑及檔名
RefreshViewer;
end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
SaveDialog1.FileName := Edit1.Text;
if SaveDialog1.Execute then
begin
FXmlTool.SaveToFile(SaveDialog1.FileName);
Edit1.Text := SaveDialog1.FileName; //算是另存新檔的感覺,所以要更名,但覆蓋並不顯示喔!
end;
end;

end.

沒有留言: