XML文件内容;
- <?xml version="1.0" encoding="GB2312"?>
- <我的书>
-
- <漫画 作者="小飞">
- <书名>火影忍者</书名>
- <价格>100</价格>
- </漫画>
- <漫画 作者="大飞">
- <书名>死神</书名>
- <价格>100</价格>
- </漫画>
- <漫画 作者="阿斗">
- <书名>天牢</书名>
- <价格>200</价格>
- </漫画>
- <小说 作者="阿斗">
- <书名>天牢</书名>
- <价格>200</价格>
- </小说>
- </我的书>
////////////
delphi内容; - unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Memo1: TMemo;
- XMLDocument1: TXMLDocument;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- mybook = class
- name: string;
- money: string;
- author: string;
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- root: IXMLnode;
- lei: IXMLNode;
- info: IXMLNode;
- book: mybook;
- i: integer;
- begin
- xmldocument1.LoadFromFile('xml.xml');
- root := xmldocument1.DocumentElement;
- lei := root.ChildNodes.First;
- while lei <> nil do
- begin
- if lei.NodeName = '漫画' then
- begin
- book := mybook.Create;
- book.author := lei.Attributes['作者'];
- info := lei.ChildNodes.First;
- while info <> nil do
- begin
- if info.NodeName = '书名' then
- book.name := info.Text
- else if info.NodeName = '价格' then
- book.money := info.Text;
- info := info.NextSibling;
- //showmessage('中华人民共和国');
- end;
- memo1.Lines.Add(book.name + book.money + book.author);
- end;
- lei := lei.NextSibling;
- end;
- end;
- end.
写XML - procedure TForm1.btn2Click(Sender: TObject);
- var
- a, b, c:IXMLNode;
- begin
- xmlDocument.Active := true;
- xmlDocument.Version := '1.0';
- xmlDocument.Encoding := 'gb2312';
- a := xmlDocument.AddChild('第一个节点');
- b := a.AddChild('第一个节点的子节点');
- c := b.AddChild('第一个节点的子节点的子节点');
- c.Text := '第一个节点的子节点的子节点的标题';
- xmlDocument.SaveToFile('e:\\pas\\Rule.xml');
- end;
|