XML文件內(nèi)容;
- <?xml version="1.0" encoding="GB2312"?>
- <我的書(shū)>
-
- <漫畫(huà) 作者="小飛">
- <書(shū)名>火影忍者</書(shū)名>
- <價(jià)格>100</價(jià)格>
- </漫畫(huà)>
- <漫畫(huà) 作者="大飛">
- <書(shū)名>死神</書(shū)名>
- <價(jià)格>100</價(jià)格>
- </漫畫(huà)>
- <漫畫(huà) 作者="阿斗">
- <書(shū)名>天牢</書(shū)名>
- <價(jià)格>200</價(jià)格>
- </漫畫(huà)>
- <小說(shuō) 作者="阿斗">
- <書(shū)名>天牢</書(shū)名>
- <價(jià)格>200</價(jià)格>
- </小說(shuō)>
- </我的書(shū)>
////////////
delphi內(nèi)容; - 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 = '漫畫(huà)' then
- begin
- book := mybook.Create;
- book.author := lei.Attributes['作者'];
- info := lei.ChildNodes.First;
- while info <> nil do
- begin
- if info.NodeName = '書(shū)名' then
- book.name := info.Text
- else if info.NodeName = '價(jià)格' then
- book.money := info.Text;
- info := info.NextSibling;
- //showmessage('中華人民共和國(guó)');
- end;
- memo1.Lines.Add(book.name + book.money + book.author);
- end;
- lei := lei.NextSibling;
- end;
- end;
- end.
寫(xiě)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('第一個(gè)節(jié)點(diǎn)');
- b := a.AddChild('第一個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn)');
- c := b.AddChild('第一個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn)的子節(jié)點(diǎn)');
- c.Text := '第一個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn)的子節(jié)點(diǎn)的標(biāo)題';
- xmlDocument.SaveToFile('e:\\pas\\Rule.xml');
- end;
|