自:http://www.delphibbs.com/keylife/iblog_show.asp?xid=20713
================================================================
-- :: xml基础操作实例,因为刚开始学,如果有不对的地方,请批评指正,代码如下: unit XMLOptionUnit;
//==============================================================================
//本实例演示
//1,XML 创建,打开,关闭操作
//2,XML 填加,添加到指定位置,删除,修改(替换),查找等操作
//作者:cactus123456@hotmail.com
//日期:2005.9.23
//版本:1.0
//==============================================================================
interface uses
SysUtils,ActiveX,MSXML2_TLB; type
RecUser=Record
U_Id :widestring;
U_Name :widestring;
U_Sex :widestring;
U_Birth :widestring;
U_Tel :widestring;
U_Addr :widestring;
U_PostCode :widestring;
U_Email :widestring;
end; type
TXMLOption=class
private
FActive :boolean;
FFilename: string;
FXMLDoc :IXMLDOMDocument;
//填加一个子节点
procedure AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
public
procedure CreateBlank(Filename: string);
procedure OpenXml(Filename: string);
procedure CloseXml;
procedure AppendUser(muser:RecUser);
procedure InsertUser(uid:string;muser:RecUser);
procedure RemoveUser(uid:string);
procedure ReplaceUser(uid:string;newuser:RecUser);
function FindUser(userid:widestring):boolean;
end; implementation const
XMLTag = 'xml';
XMLPrologAttrs = 'version="1.0" encoding="UTF-8"';
XMLComment = '简单XML文档操作用户实例'# +
'用户结构为序号,姓名,性别,出生年月日,电话,住址,邮编,电邮'# +
'作者 cactus123456@hotmail.com, 2005.9.21';
UserWatcherTag = 'user-watcher';
XMLComment2 = '创建文档时间:';
UsersTag = 'users';
U_Id = 'id';
U_Name = 'name';
U_Sex = 'sex';
U_Birth = 'birth';
U_Tel = 'tel';
U_Addr = 'addr';
U_PostCode = 'postcode';
U_Email = 'email'; //创建一个空XML,如果这个Filename文件已经存在,则覆盖
procedure TXMLOption.CreateBlank(Filename: string);
begin
FActive:=false;
FFilename:='';
try
FXMLDoc := CoDOMDocument.Create;
FXMLDoc.AppendChild(FXMLDoc.CreateProcessingInstruction(XMLTag, XMLPrologAttrs));
FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment));
FXMLDoc.AppendChild(FXMLDoc.CreateElement(UserWatcherTag));
FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment2+datetimetostr(now)));
FXMLDoc.save(Filename);
FFilename:=Filename;
FActive:=true;
except
FXMLDoc:=nil;
end;
end;
//打开一个存在的Filename XML文档
procedure TXMLOption.OpenXml(Filename: string);
begin
if not Assigned(FXMLDoc) then
begin
FXMLDoc := CoDOMDocument.Create;
if FXMLDoc.Load(Filename) then FActive:=true
else FActive:=false;
if FActive then FFilename:=Filename
else FFilename:='';
end;
end;
//关闭一个打开的XML文档
procedure TXMLOption.CloseXml;
begin
if Assigned(FXMLDoc) then FXMLDoc:=nil;
FFilename:='';
FActive:=false;
end;
procedure TXMLOption.AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
var
Internal: IXMLDOMElement;
begin
Internal:=IXMLDOMElement(Parent.AppendChild(FXMLDoc.CreateElement(Field)));
Internal.AppendChild(FXMLDoc.CreateTextNode(Value));
end;
//填加一个节点到后面
procedure TXMLOption.AppendUser(muser:RecUser);
var
xuser:IXMLDOMElement;
xroot:IXMLDOMElement;
begin
if FActive then
begin
xroot:=FXMLDoc.documentElement;
xuser :=IXMLDOMElement(xroot.AppendChild(FXMLDoc.CreateElement(UsersTag)));
AddSimpleElement(xuser,U_Id,muser.U_Id);
AddSimpleElement(xuser,U_Name,muser.U_Name);
AddSimpleElement(xuser,U_Sex,muser.U_Sex);
AddSimpleElement(xuser,U_Birth,muser.U_Birth);
AddSimpleElement(xuser,U_Tel,muser.U_Tel);
AddSimpleElement(xuser,U_Addr,muser.U_Addr);
AddSimpleElement(xuser,U_PostCode,muser.U_PostCode);
AddSimpleElement(xuser,U_Email,muser.U_Email);
FXMLDoc.save(FFilename);
end;
end;
procedure TXMLOption.InsertUser(uid:string;muser:RecUser);
var
xfind:IXMLDOMNode;
xuser:IXMLDOMElement;
xroot:IXMLDOMElement;
xpath:string;
begin
if not FActive then exit;
xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
//如果没有找到, xfind=nil 则在文件的末尾插入
//如果找到,xfind<>nil 则在找到的纪录前面插入
xroot:=FXMLDoc.documentElement;
xuser :=IXMLDOMElement(xroot.insertBefore(FXMLDoc.CreateElement(UsersTag),xfind));
AddSimpleElement(xuser,U_Id,muser.U_Id);
AddSimpleElement(xuser,U_Name,muser.U_Name);
AddSimpleElement(xuser,U_Sex,muser.U_Sex);
AddSimpleElement(xuser,U_Birth,muser.U_Birth);
AddSimpleElement(xuser,U_Tel,muser.U_Tel);
AddSimpleElement(xuser,U_Addr,muser.U_Addr);
AddSimpleElement(xuser,U_PostCode,muser.U_PostCode);
AddSimpleElement(xuser,U_Email,muser.U_Email);
FXMLDoc.save(FFilename);
end;
procedure TXMLOption.RemoveUser(uid:string);
var
xfind:IXMLDOMNode;
xroot:IXMLDOMElement;
xpath:string;
begin
if not FActive then exit;
xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
if xfind<>nil then
begin
xroot:=FXMLDoc.documentElement;
xroot.removeChild(xfind);
FXMLDoc.save(FFilename);
end;
end;
procedure TXMLOption.ReplaceUser(uid:string;newuser:RecUser);
var
xfind,newnode:IXMLDOMNode;
xroot:IXMLDOMElement;
xpath:string;
begin
if not FActive then exit;
xpath:=UsersTag+'['+U_Id+'="'+uid+'"]';
xfind:=FXMLDoc.documentElement.selectSingleNode(xpath);
//如果没有找到,则不做替换
if xfind<>nil then
begin
newnode:=xfind.cloneNode(true);
newnode.selectSingleNode(U_Id).text:=newuser.U_Id;
newnode.selectSingleNode(U_Name).text:=newuser.U_Name;
newnode.selectSingleNode(U_Sex).text:=newuser.U_Sex;
newnode.selectSingleNode(U_Birth).text:=newuser.U_Birth;
newnode.selectSingleNode(U_Tel).text:=newuser.U_Tel;
newnode.selectSingleNode(U_Addr).text:=newuser.U_Addr;
newnode.selectSingleNode(U_PostCode).text:=newuser.U_PostCode;
newnode.selectSingleNode(U_Email).text:=newuser.U_Email;
xroot:=FXMLDoc.documentElement;
xroot.replaceChild(newnode,xfind);
FXMLDoc.save(FFilename);
end;
end;
function TXMLOption.FindUser(userid:widestring):boolean;
var
xuser:IXMLDOMNode;
xpath:string;
begin
result:=false;
if not FActive then exit;
//关于xpath语法说明,参见www.w3.org/TR/xpath
xpath:=UsersTag+'['+U_Id+'="'+userid+'"]';
xuser:=FXMLDoc.documentElement.selectSingleNode(xpath);
if xuser<>nil then result:=true;
end; initialization
{ Initialise COM }
CoInitialize(nil);
finalization
{ Tidy up }
CoUninitialize(); end. 调用上面单元的实例的代码,unit单元: unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,XMLOptionUnit, OleCtrls, SHDocVw; type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Button3: TButton;
Button4: TButton;
Button5: TButton;
WebBrowser1: TWebBrowser;
Label1: TLabel;
Button6: TButton;
Button7: TButton;
Button8: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
{ Private declarations }
FXMLOption:TXMLOption;
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject);
begin
FXMLOption:=TXMLOption.Create;
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
FXMLOption.Free;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
FXMLOption.CreateBlank(edit1.Text);
end; procedure TForm1.Button2Click(Sender: TObject);
var
auser:RecUser;
begin
auser.U_Id:=edit2.Text;
auser.U_Name:='tom';
auser.U_Sex:='男';
auser.U_Birth:='1979-8-7';
auser.U_Tel:='';
auser.U_Addr:='tom 大街 8 号';
auser.U_PostCode:='';
auser.U_Email:='tom@888.com';
FXMLOption.AppendUser(auser);
WebBrowser1.Navigate(ExtractFilePath(application.ExeName)+edit1.Text);
end; procedure TForm1.Button3Click(Sender: TObject);
begin
FXMLOption.OpenXml(edit1.Text);
WebBrowser1.Navigate(ExtractFilePath(application.ExeName)+edit1.Text);
end; procedure TForm1.Button4Click(Sender: TObject);
begin
FXMLOption.CloseXml;
WebBrowser1.Navigate('about:blank');
end; procedure TForm1.Button5Click(Sender: TObject);
begin
if FXMLOption.FindUser(edit2.text) then label1.Caption:='true'
else label1.Caption:='false';
end; procedure TForm1.Button6Click(Sender: TObject);
var
auser:RecUser;
begin
auser.U_Id:=edit2.Text;
auser.U_Name:='peter';
auser.U_Sex:='女';
auser.U_Birth:='1980-8-7';
auser.U_Tel:='36-3654-7890';
auser.U_Addr:='peter 大街 8 号';
auser.U_PostCode:='';
auser.U_Email:='peter@888.com';
FXMLOption.InsertUser(edit2.text,auser);
WebBrowser1.Navigate(ExtractFilePath(application.ExeName)+edit1.Text);
end; procedure TForm1.Button7Click(Sender: TObject);
begin
FXMLOption.RemoveUser(edit2.text);
WebBrowser1.Navigate(ExtractFilePath(application.ExeName)+edit1.Text);
end; procedure TForm1.Button8Click(Sender: TObject);
var
auser:RecUser;
begin
auser.U_Id:=edit2.Text;
auser.U_Name:='张三';
auser.U_Sex:='男';
auser.U_Birth:='1970-8-7';
auser.U_Tel:='001654-7890';
auser.U_Addr:='张三 大街 8 号';
auser.U_PostCode:='';
auser.U_Email:='zhangsan@888.com';
FXMLOption.ReplaceUser(edit2.Text,auser);
WebBrowser1.Navigate(ExtractFilePath(application.ExeName)+edit1.Text);
end; end. Unit单元对应的Form: object Form1: TForm1
Left =
Top =
Width =
Height =
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch =
TextHeight =
object Label1: TLabel
Left =
Top =
Width =
Height =
Caption = 'Label1'
end
object Button1: TButton
Left =
Top =
Width =
Height =
Caption = 'CreateBlank'
TabOrder =
OnClick = Button1Click
end
object Button2: TButton
Left =
Top =
Width =
Height =
Caption = 'AddUser'
TabOrder =
OnClick = Button2Click
end
object Edit1: TEdit
Left =
Top =
Width =
Height =
TabOrder =
Text = 'userxml.xml'
end
object Edit2: TEdit
Left =
Top =
Width =
Height =
TabOrder =
Text = ''
end
object Button3: TButton
Left =
Top =
Width =
Height =
Caption = 'OpenXml'
TabOrder =
OnClick = Button3Click
end
object Button4: TButton
Left =
Top =
Width =
Height =
Caption = 'CloseXml'
TabOrder =
OnClick = Button4Click
end
object Button5: TButton
Left =
Top =
Width =
Height =
Caption = 'FindUser'
TabOrder =
OnClick = Button5Click
end
object WebBrowser1: TWebBrowser
Left =
Top =
Width =
Height =
Align = alTop
TabOrder =
ControlData = {
4C0000001B470000592000000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object Button6: TButton
Left =
Top =
Width =
Height =
Caption = 'InsertUser'
TabOrder =
OnClick = Button6Click
end
object Button7: TButton
Left =
Top =
Width =
Height =
Caption = 'RemoveUser'
TabOrder =
OnClick = Button7Click
end
object Button8: TButton
Left =
Top =
Width =
Height =
Caption = 'ReplaceUser'
TabOrder =
OnClick = Button8Click
end
end

http://blog.csdn.net/genispan/article/details/4364492

以上XPATH有误 应改为:
xpath:=UsersTag '[@' U_Id '=&quot;' userid '&quot;]';

delphi 操作xml示例(DelphiBBS)的更多相关文章

  1. Delphi操作XML

    Delphi操作XML Delphi操作XMl,只要使用 NativeXml.我是用的版本是4..NativeXML的使用方法比较简单,但是功能很强大. XE2的话,要在simdesign.inc后面 ...

  2. Delphi操作XML - 冰雪傲骨

    Delphi操作XMl,只要使用 NativeXml.我是用的版本是4..NativeXML的使用方法比较简单,但是功能很强大. XE2的话,要在simdesign.inc后面加上: // Delph ...

  3. delphi操作xml学习笔记 之一 入门必读

    Delphi 对XML的支持---TXMLDocument类       Delphi7 支持对XML文档的操作,可以通过TXMLDocument类来实现对XML文档的读写.可以利用TXMLDocum ...

  4. Delphi操作XML简介

    参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...

  5. Delphi操作XML的几个博客

    http://www.cnblogs.com/acuier  整整十几篇,省得我自己研究,学一下就可以了. http://www.cnblogs.com/del/category/113561.htm ...

  6. Delphi ServerSocket,ClientSocket示例

    Delphi ServerSocket,ClientSocket示例 2008-05-09 16:20 Delphi TServerSocket,TClientSocket实现传送文件代码 1.建立两 ...

  7. 操作xml文档的常用方式

    1.操作XML文档的两种常用方式: 1)使用XmlReader类和XmlWriter类操作 XmlReader是基于数据流的,占用极少的内存,是只读方式的,所以速度极快.只能采用遍历的模式查找数据节点 ...

  8. Java原生API操作XML

    使用Java操作XML的开源框架比较多,如著名的Dom4J.JDOM等,但个人认为不管你用那个框架都要对JDK原生的API有所了解才能更得心应手的应用.本篇就来简单了解下原生的XML API. JAV ...

  9. PHP原生DOM对象操作XML'代码'

    对于操作XML类型文件,PHP内置有一套DOM对象可以进行处理.对XML的操作,从创建.添加到修改.删除都可以使用DOM对象中的函数来进行. 创建 创建一个新的XML文件,并且写入一些数据到这个XML ...

随机推荐

  1. 微信开发学习日记(六):weiphp框架

    最近重点在看weiphp这个开源的第三方微信公众平台框架. 在网上找微信资料,找到了这个.很早之前,就初步学习了Thinkphp和Onethink2个开源框架,当看到weiphp是用这2个框架开发的时 ...

  2. [CSS] Re-order the appearance of grid items using the order property

    As with flex items, we can set an order value on grid items. Let’s see how this affects the DOM and ...

  3. Mysql用户本机登陆不成功的解决

    mysql新建一个用户,本机不能登陆,但是远程能够登陆,不知什么原因,最后查阅 http://blog.itpub.net/12679300/viewspace-1453490/ 这篇文章得以解决,进 ...

  4. 【hdu 2177】取(2堆)石子游戏

    Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  5. Java实现的并发任务处理实例

    本文实例讲述了Java实现的并发任务处理方法.分享给大家供大家参考,具体如下: public void init() { super.init(); this.ioThreadPool = new T ...

  6. WPF实现射线效果动画

    原文:WPF实现射线效果动画 最近的一个项目中有个需求是:从一个点向其它多个点发出射线,要求这些射线同时发出,同时到达. 我就想到了用WPF的动画来实现.WPF中有Line类用于绘制直线,但这个类中好 ...

  7. HTML5在客户端存储数据的新方法——localStorage

    HTML5在客户端存储数据的新方法--localStorage localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上 ...

  8. Linux下的正斜杠"/"和"\"的区别

    今天在检查root目录时发现有一个名为"\"的文件,觉得很奇怪,从来没见过,就准备用Vim打开看看,很自然地输入命令查看一下,结果居然打不开. [root@localhost ~] ...

  9. 实战caffe多标签分类——汽车品牌与车辆外观(C++接口)[详细实现+数据集]

    前言 很多地方我们都需要用到多标签分类,比如一张图片,上面有只蓝猫,另一张图片上面有一只黄狗,那么我们要识别的时候,就可以采用多标签分类这一思想了.任务一是识别出这个到底是猫还是狗?(类型)任务二是识 ...

  10. WPF的两棵树与绑定

    原文:WPF的两棵树与绑定   先建立测试基类 public class VisualPanel : FrameworkElement { protected VisualCollection Chi ...