全部代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, ComCtrls, ToolWin, StdCtrls,
ExtCtrls,RichEdit, ImgList;
{Types,RichEdit,u_keywords;}
type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TreeView1: TTreeView;
Splitter1: TSplitter;
RichEdit1: TRichEdit;
StatusBar1: TStatusBar;
ToolBar1: TToolBar;
LoadToolBtn: TToolButton;
XMLDocument1: TXMLDocument;
CancelToolBtn: TToolButton;
SaveAllToolBtn: TToolButton;
ImageList1: TImageList;
procedure LoadToolBtnClick(Sender: TObject);
procedure RichEdit1KeyPress(Sender: TObject; var Key: Char);
procedure SaveAllToolBtnClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure RichEdit1Enter(Sender: TObject);
procedure RichEdit1Change(Sender: TObject);
procedure RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure CancelToolBtnClick(Sender: TObject);
private
{ Private declarations }
//function getXY:TPoint;
public
{ Public declarations }
function myCheckNodeType(aNodeType:TNodeType):Integer;
procedure myOutputNode(aChildNode:IXMLNode;level:Integer);
procedure myOutputEnd(aChildNode:IXMLNode;level:Integer);
procedure VisitChild(aNode:IXMLNode;level:Integer);
function myOutPutTreeNode(aXMLNode:IXMLNode):string;
procedure BuildTree(aChild:IXMLNode;Parent:TTreeNode);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tform1.myOutputNode(aChildNode:IXMLNode;level:Integer);
function indent(level:Integer):Integer;
var
//s:string;
i:Integer;
begin
{s:='';
for i := 0 to level do
begin
s:=s+' ';
end;
Result:=Copy(s,0,Length(s)-4); }
Result:=level*20;
end;
var
i:Integer;
CDATAList:TStringList;
//CharFmt:TCharFormat2;
begin
RichEdit1.Paragraph.Alignment:=taLeftJustify;
RichEdit1.Paragraph.FirstIndent:=indent(level);
RichEdit1.Paragraph.LeftIndent:=indent(level);
case myCheckNodeType(aChildNode.NodeType) of
0:;//do nothing
1:begin
RichEdit1.SelAttributes.Color:=clRed;
RichEdit1.SelAttributes.Style:=[fsBold];
RichEdit1.Lines.Add({indent(level)+}'<'+aChildNode.NodeName);
if aChildNode.AttributeNodes.Count>0 then
begin
for i := 0 to aChildNode.AttributeNodes.Count-1 do
begin
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]
+' '
+aChildNode.AttributeNodes
.NodeName+'="'
+VarToStr(aChildNode.AttributeNodes.NodeValue+'"');
end;
end;
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]:=
RichEdit1.Lines.Strings[RichEdit1.Lines.Count-1]+'>';
end;
2:;//do nothing
3:begin
RichEdit1.SelAttributes.Color:=clBlack;
RichEdit1.SelAttributes.Style:=[fsBold];
RichEdit1.Lines.Add({indent(level)+}aChildNode.Text);
end;
4:begin
CDATAList:=TStringList.Create;
//CDATAList.Assign()
//
CDATAList.Text:='<![CDATA['+VarToStr(aChildNode.NodeValue)+']]>';
for i := 0 to CDATAList.Count-1 do
begin
if i=0 then
begin
RichEdit1.SelAttributes.Color:=clBlue;
RichEdit1.SelAttributes.Style:=[fsBold];
RichEdit1.Lines.Add({indent(level)+}CDATAList.Strings);
end
else if i=CDATAList.Count-1 then
begin
RichEdit1.SelAttributes.Color:=clBlue;
RichEdit1.SelAttributes.Style:=[fsBold];
RichEdit1.Lines.Add({indent(level)+' '+}CDATAList.Strings);
end
else
begin
RichEdit1.SelAttributes.Color:=clBlack;
RichEdit1.SelAttributes.Style:=[];
RichEdit1.Lines.Add({indent(level)+' '+}CDATAList.Strings);
end;
end;
CDATAList.Free;
end;
5:;//do nothing
6:;//do nothing
7:begin
RichEdit1.SelAttributes.Color:=clBlue;
RichEdit1.Lines.Add({indent(level)+}'<?xml '+VarToStr(aChildNode.NodeValue)+'?>');
end;
8:begin
RichEdit1.SelAttributes.Color:=clGrayText;
RichEdit1.Lines.Add({indent(level)+}'<!--'+VarToStr(aChildNode.NodeValue)+
'-->');
end;
9:;
10:;
11:;
12:;
end;
end;
function tform1.myCheckNodeType(aNodeType:TNodeType):Integer;
begin
if aNodeType=ntReserved then Result:=0;
if aNodeType=ntElement then Result:=1;
if aNodeType=ntAttribute then Result:=2;
if aNodeType=ntText then Result:=3;
if aNodeType=ntCData then Result:=4;
if aNodeType=ntEntityRef then Result:=5;
if aNodeType=ntEntity then Result:=6;
if aNodeType=ntProcessingInstr then Result:=7;
if aNodeType=ntComment then Result:=8;
if aNodeType=ntDocument then Result:=9;
if aNodeType=ntDocType then Result:=10;
if aNodeType=ntDocFragment then Result:=11;
if aNodeType=ntNotation then Result:=12;
end;
procedure TForm1.LoadToolBtnClick(Sender: TObject);
begin
XMLDocument1.LoadFromFile('C:/Documents and Settings/Admin/桌面/a.xml');
XMLDocument1.Active:=True;
if XMLDocument1.IsEmptyDoc then Exit;
if not XMLDocument1.Active then Exit;
RichEdit1.Lines.Clear;
TreeView1.Items.Clear;
BuildTree(XMLDocument1.DocumentElement.ParentNode,nil);
VisitChild(XMLDocument1.DocumentElement.ParentNode,0);
SaveAllToolBtn.Enabled:=False;
CancelToolBtn.Enabled:=False;
StatusBar1.Panels[2].Text:='字节数:'+IntToStr(Length(RichEdit1.Text));
end;
procedure TForm1.VisitChild(aNode: IXMLNode;level:Integer); //先序遍历
var
i:Integer;
begin
if aNode<>nil then
begin
myOutputNode(aNode,level);
for i := 0 to aNode.ChildNodes.Count-1 do
begin
VisitChild(aNode.ChildNodes,level+1);
end;
myOutputEnd(aNode,level);
end;
end;
procedure TForm1.myOutputEnd(aChildNode: IXMLNode; level: Integer);
function indent(level:Integer):Integer;
var
//s:string;
i:Integer;
begin
{s:='';
for i := 0 to level do
begin
s:=s+' ';
end;
Result:=Copy(s,0,Length(s)-4); }
Result:=level*20;
end;
begin
case myCheckNodeType(aChildNode.NodeType) of
0:;
1:begin
RichEdit1.Paragraph.Alignment:=taLeftJustify;
RichEdit1.Paragraph.FirstIndent:=indent(level);
RichEdit1.Paragraph.LeftIndent:=indent(level);
RichEdit1.SelAttributes.Color:=clRed;
RichEdit1.SelAttributes.Style:=[fsBold];
RichEdit1.Lines.Add({indent(level)+}'</'+aChildNode.NodeName+'>');
end;
2:;
3:;
4:;
5:;
6:;
7:;
8:;
9:;
10:;
11:;
12:;
end;
end;
{function TForm1.getXY: TPoint;
begin
//result.Y:=SendMessage(Handle, EM_EXLINEFROMCHAR, 0,SelStart);
//result.X:=(SelStart - SendMessage(Handle, EM_LINEINDEX, result.Y, 0));
end; }
procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
var
x,y:Integer;
begin
Y:=RichEdit1.Perform(EM_EXLINEFROMCHAR, 0,richedit1.SelStart);
X:=(RichEdit1.SelStart - RichEdit1.Perform(EM_LINEINDEX, Y, 0));
StatusBar1.Panels[1].Text:='行:'+IntToStr+',列:'+IntToStr(x);
if Key=#13 then
begin
{j:=0;
repeat
if s[j]<>' ' then
begin
Break;
end;
j:=j+1;
until i>Length(s);
for c := 1 to j do
begin
RichEdit1.Lines.Strings[i+1]:=' '+RichEdit1.Lines.Strings[i+1];
end;}
end;
end;
procedure TForm1.BuildTree(aChild: IXMLNode; Parent: TTreeNode);//先序建立结点树
var
aTreeNode:TTreeNode;
i:Integer;
begin
if aChild<>nil then
begin
aTreeNode:=TreeView1.Items.AddChild(Parent,myOutPutTreeNode(aChild));
aTreeNode.ImageIndex:=myCheckNodeType(aChild.NodeType);
aTreeNode.SelectedIndex:=myCheckNodeType(aChild.NodeType);
if myCheckNodeType(aChild.NodeType)=1 then
begin
aTreeNode.SelectedIndex:=13;
end;
aTreeNode.Data:=@aChild;
for i := 0 to aChild.ChildNodes.Count-1 do
begin
BuildTree(aChild.ChildNodes,aTreeNode);
end;
end;
end;
function TForm1.myOutPutTreeNode(aXMLNode: IXMLNode):string;
begin
case myCheckNodeType(aXMLNode.NodeType) of
0:Result:=aXMLNode.NodeName;
1:Result:='<'+aXMLNode.NodeName+'>';
2:Result:=aXMLNode.NodeName;
3:Result:=aXMLNode.NodeName;
4:Result:=aXMLNode.NodeName;
5:Result:=aXMLNode.NodeName;
6:Result:=aXMLNode.NodeName;
7:Result:='<?'+aXMLNode.NodeName+'...?>';
8:Result:='<!-- '+aXMLNode.NodeName+'...-->';
9:Result:=aXMLNode.NodeName;
10:Result:=aXMLNode.NodeName;
11:Result:=aXMLNode.NodeName;
12:Result:=aXMLNode.NodeName;
end;
end;
procedure TForm1.SaveAllToolBtnClick(Sender: TObject);
var
i:Integer;
begin
XMLDocument1.XML.Clear;
for i := 0 to RichEdit1.Lines.Count-1 do
begin
XMLDocument1.XML.Add(Trim(RichEdit1.Lines.Strings));
end;
//XMLDocument1.XML.Append('<!--北京瑞德高科技术有限公司 www.x-sql.com-->');
XMLDocument1.Active:=True;
RichEdit1.Lines.Clear;
TreeView1.Items.Clear;
if XMLDocument1.IsEmptyDoc then Exit;
if not XMLDocument1.Active then Exit;
BuildTree(XMLDocument1.DocumentElement.ParentNode,nil);
VisitChild(XMLDocument1.DocumentElement.ParentNode,0);
XMLDocument1.XML.SaveToFile('C:/Documents and Settings/Admin/桌面/w.xml');
CancelToolBtn.Enabled:=False;
SaveAllToolBtn.Enabled:=False;
end;
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
i:Integer;
//search:string;
//aNode:TTreeNode;
begin
case TreeView1.Selected.ImageIndex of
0:;
1:begin
for i := 0 to TreeView1.Selected.Level do
begin
;
end;
end;
2:;
3:;
end;
end;
procedure TForm1.RichEdit1Enter(Sender: TObject);
var
x,y:Integer;
begin
Y:=RichEdit1.Perform(EM_EXLINEFROMCHAR, 0,richedit1.SelStart);
X:=(RichEdit1.SelStart - RichEdit1.Perform(EM_LINEINDEX, Y, 0));
StatusBar1.Panels[1].Text:='行:'+IntToStr+',列:'+IntToStr(x);
end;
procedure TForm1.RichEdit1Change(Sender: TObject);
var
x,y:Integer;
begin
CancelToolBtn.Enabled:=True;
SaveAllToolBtn.Enabled:=True;
Y:=RichEdit1.Perform(EM_EXLINEFROMCHAR, 0,richedit1.SelStart);
X:=(RichEdit1.SelStart - RichEdit1.Perform(EM_LINEINDEX, Y, 0));
StatusBar1.Panels[1].Text:='行:'+IntToStr+',列:'+IntToStr(x);
StatusBar1.Panels[2].Text:='字节数:'+IntToStr(Length(RichEdit1.Text));
end;
procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
x1,y1:Integer;
begin
Y1:=RichEdit1.Perform(EM_EXLINEFROMCHAR, 0,richedit1.SelStart);
X1:=(RichEdit1.SelStart - RichEdit1.Perform(EM_LINEINDEX, Y1, 0));
StatusBar1.Panels[1].Text:='行:'+IntToStr(y1)+',列:'+IntToStr(x1);
end;
procedure TForm1.CancelToolBtnClick(Sender: TObject);
begin
LoadToolBtn.Click;
SaveAllToolBtn.Enabled:=False;
CancelToolBtn.Enabled:=False;
end;
end.