使用Delphi创建,解析,操纵XML文档
http://delphi.about.com/od/windowsshellapi/a/xml_delphi.htm
Creating, Parsing and Manipulating XML Documents with Delphi
Delphi and the Extensible Markup Language
By Zarko Gajic, About.com Guide
使用Delphi创建,解析,操纵XML文档
What is XML?
XML is the 'Extensible Markup Language', universal language for data on the Web. XML gives developers the power to deliver structured data from a wide variety of applications to the desktop for local computation and presentation. XML is also an ideal format for server-to-server transfer of structured data. Using an XML parser, software is able to walk through the hierarchy of the document extracting the structure of the document, its content, or both. XML is in no way limited to Internet use. In fact, XML's main strength - organizing information - makes it perfect for exchanging data between different systems
XML looks much like HTML. However, whereas HTML describes the layout of a page (for data displaying purposes) XML is concerned with defining and describing data, it describes the type of content. Hence, 'extensible' because it is not a fixed format like HTML.
We can also think of each XML file as a mini database. Tag, or markups (the text surrounded by angle brackets) in an XML document delineate the records and fields. The text between the tags is the data. We perform operations like retrieving, updating, and inserting data with XML using a parser and a set of objects exposed by the parser.
As a Delphi programmer, what you need to know is how to use XML. Therefore, in this article I'll be presenting several ways in which XML can be integrated into your Delphi applications.
什么是XML
XML是可扩展的标记语言,网络上数据的公共语言。XML给开发者传递结构化的数据从品种繁多的应用程序到桌面以供本地计算和展示的能力。
XLM也是一个从服务器到服务器端的结构化数据传递的理想的格式.使用XML解析器,软件可以轻而易举地通过文档的组织结构解析文档的结构,它的内容,或者两者。XML决不限制于在Internet上使用,事实上,XML的主要长处-组织信息-使非常完美的在不同的两个系统间交换数据。XML十分像HTML,然而,尽管HTML描述了页面的页面的布局(为显示数据)XML关注于定义和描述数据,它描述内容的类型.因此,可扩展性是因为它不像HTML一样是固定的。
我们也可以认为每个XML文件都是一个小型的数据库。XML文档中的标签,标式(被尖括号包围的文本)描述记录和域,两个标签之间的文本是数据.
我们用XML解析器执行像检索,更新,和插入数据的操作并且对象的集合也是由解析器显示。
作为一个Delphi开发者,你需要做的就是知道如何去用XML,在这篇文章中,我将展示几个XML能被融合到你的Delphi程序中去的方法。
Saving TTreeView items to XML
First, we build a sample application hosting a TTreeView component, two ImageList components and one TXMLDocument component. One image list is associated with the Images property of a TreeView, another attached to the StateImages property (see: how to add check boxes and radio buttons to a tree view).
保存TTreeView项目到XML中
首先我们建立一个存放一个TTreeView组件,两个ImageList组件和一个TXMLDocument组件.一个ImageList关联到TTreeView的Image属性,另一个附加到StateImage属性(见:如何添加CheckBox和RadioButtons到TreeView中).
Procedure Tree2XML(tree: TTreeView);
var
XMLDoc: TXMLDocument;
iNode: IXMLNode;
tn: TTreeNode;
procedure ProcessTreeItem(tn: TTreeNode; iNode: IXMLNode);
var
cNode: IXMLNode;
Begin
if tn = nil then Exit;
cNode := iNode.AddChild('Item');
cNode.Attributes['text'] := tn.Text;
cNode.Attributes['ImageIndex'] := tn.ImageIndex;
cNode.Attributes['StateIndex'] := tn.StateIndex;
tn := tn.getFirstChild;
while (tn <> nil) do
Begin
ProcessTreeItem(tn, iNode);
tn := tn.getNextSibling;
end;
end;
Begin
XMLDoc := TXMLDocument.Create(nil);
XMLDoc.Active := True;
iNode := XMLDoc.AddChild('TreeToXML');
iNode.Attributes['app'] := ParamStr(0);
tn := tree.TopItem;
while tn <> nil do
Begin
ProcessTreeItem(tn, iNode);
tn := tn.getNextSibling;
end;
XMLDoc.SaveToFile(ChangeFileExt(ParamStr(0), 'XML'));
FreeAndNil(XMLDoc);
end;
Procedure XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument);
var
iNode: IXMLNode;
procedure ProcessNode(tn: TTreeNode; Node: IXMLNode);
var
cNode: IXMLNode;
Begin
if Node = nil then Exit;
tn := Tree.Items.AddChild(tn, Node.Attributes['text']);
tn.ImageIndex := Integer(Node.Attributes['imageIndex']);
tn.StateIndex := Integer(Node.Attributes['stateIndex']);
Cnode := Node.ChildNodes.First;
while (CNode <> nil) do
Begin
ProcessNode(tn, Cnode);
CNode := Cnode.NextSibling;
end;
end;
Begin
Tree.Items.Clear;
XMLDoc.FileName := ChangeFileExt(ParamStr(0), '.XML');
XMLDoc.Active := True;
INode := XMLDoc.DocumentElement.ChildNodes.First;
if INode = nil then Exit;
while (INode <> nil) do
Begin
ProcessNode(nil, INode);
INode := INode.NextSibling;
end;
FreeAndNil(XMLDoc);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
XML2Tree(tree, XMLDocument1);
end;
浙公网安备 33010602011771号