Delphi直接读写XML修改版
来源:互联网 发布:pl sql developer配置 编辑:程序博客网 时间:2024/05/16 12:35
<script type="text/javascript"><!--google_ad_client = "pub-3528650120430763";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel = "";google_ui_features = "rc:0";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3528650120430763&dt=1183799909421&lmt=1183799909&format=468x60_as&output=html&correlator=1183799909421&url=http%3A%2F%2Fpages.google.com%2Fdraft%2FGmai9999%2Fhome%3Fauthtoken%3D397572419ac6db50f5ab16961d0b13491bee0967&ad_type=text_image&ref=http%3A%2F%2Fpages.google.com%2Fpreview%2FGmai9999%2Fhome%3Fauthtoken%3D9686509b5e94c7e953ceecc7a041fbc446802a9a%26no-cache%3D15040874706351293&ui=rc%3A0&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe>
有时,只需要用XML作一些小的应用,比如只是简单地保存日志或者一些配置,这时我们只需要直接读写XML就好,效率第一。
Delphi盒子有一个直接读写XML文件 (例子和代码),其核心函数为下面两个函数(一读一写):
{-------------------------------------------------------------------------------
Fun/Pro: GetXMLNodeValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: dep 节点的参数的分隔符,默认为.
@Return: 第一个节点的值
-------------------------------------------------------------------------------}
function GetXMLNodeValue(strEntityEngineFile:String; xmlNodePath:String;
const xmlattrname:String=''; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
else Break;
end;
if(node=nil)then begin
result:='';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)='') then
result:=node.Text
else
result:=node.AttributeNodes.Nodes[xmlattrname].NodeValue;
end;
end else begin
result:='';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
{-------------------------------------------------------------------------------
Fun/Pro: SetXMLNodeValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: dep 节点的参数的分隔符,默认为.
@Return: 操作成功否
-------------------------------------------------------------------------------}
function setXmlNodeValue(strEntityEngineFile:String; xmlNodePath:String;
const xmlattrname:String=''; const value:String=''; const dep:Char ='.'):boolean;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
else Break;
end;
if(node <> nil)then begin
if(Trim(xmlattrname)='') then
node.Text:=value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value;
xmlDocument.SaveToFile(strEntityEngineFile);
end;
end;
result:=true;
except
result:=false;
end;
xmlDocument.Active:=false;
end;
但是上述两个函数有一个问题:它只能按节点名和属性名查找第一条记录。举例:如果要操作类似下述XML文件,节点和属性名相同的有多个,只是属性的值不一样,上面的读写函数就罢工了。
<colour name="normal attribute" red="100" green="125" blue="150"/>
<colour name="good attribute" red="150" green="175" blue="200"/>
<colour name="excellent attribute" red="0" green="0" blue="255"/>
<colour name="good attribute" red="150" green="175" blue="200"/>
<colour name="excellent attribute" red="0" green="0" blue="255"/>
在原有函数的基础上增加了两个参数:
{-------------------------------------------------------------------------------
Fun/Pro: GetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 某属性的值
-------------------------------------------------------------------------------}
function GetXMLNodeSpecialValue(strEntityEngineFile:String; XMLNodePath:String;
const XMLAttrName:String=''; const XMLSpecialName:String=''; const XMLSpecialValue:String=''; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
begin
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i]);
end
else Break;
end;
if(node=nil)then begin
result:='';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)='') then
result:=node.Text
else
begin
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue; //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。
while ((result <> XMLSpecialValue)) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
result:=node.AttributeNodes.Nodes[XMLAttrName].NodeValue;
end;
end;
end else begin
result:='';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
Fun/Pro: GetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 某属性的值
-------------------------------------------------------------------------------}
function GetXMLNodeSpecialValue(strEntityEngineFile:String; XMLNodePath:String;
const XMLAttrName:String=''; const XMLSpecialName:String=''; const XMLSpecialValue:String=''; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
begin
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i]);
end
else Break;
end;
if(node=nil)then begin
result:='';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)='') then
result:=node.Text
else
begin
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue; //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。
while ((result <> XMLSpecialValue)) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
result := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
result:=node.AttributeNodes.Nodes[XMLAttrName].NodeValue;
end;
end;
end else begin
result:='';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
写函数
{-------------------------------------------------------------------------------
Fun/Pro: SetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 操作成功与否
-------------------------------------------------------------------------------}
function SetXMLNodeSpecialValue(strEntityEngineFile:String; xmlNodePath:String;
const xmlattrname:String=''; const value:String=''; const XMLSpecialName:String=''; const XMLSpecialValue:String=''; const dep:Char ='.'):boolean;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
CMPValue :String;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
else Break;
end;
if(node <> nil)then begin
{if(Trim(xmlattrname)='') then
node.Text:=value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value;
}
if (Trim(XMLAttrName)='') then
node.Text := value
else
begin
CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
while (CMPValue <> XMLSpecialValue) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
node.AttributeNodes.Nodes[XMLAttrName].NodeValue:=value;
end;
xmlDocument.SaveToFile(strEntityEngineFile);
end;
end;
result:=true;
except
result:=false;
end;
xmlDocument.Active:=false;
end;
Fun/Pro: SetXMLNodeSpecialValue
@Date: 2004.12.11
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialValue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 操作成功与否
-------------------------------------------------------------------------------}
function SetXMLNodeSpecialValue(strEntityEngineFile:String; xmlNodePath:String;
const xmlattrname:String=''; const value:String=''; const XMLSpecialName:String=''; const XMLSpecialValue:String=''; const dep:Char ='.'):boolean;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
CMPValue :String;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i])
else Break;
end;
if(node <> nil)then begin
{if(Trim(xmlattrname)='') then
node.Text:=value
else
node.AttributeNodes.Nodes[xmlattrname].NodeValue:=value;
}
if (Trim(XMLAttrName)='') then
node.Text := value
else
begin
CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
while (CMPValue <> XMLSpecialValue) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
CMPValue := node.AttributeNodes.Nodes[XMLSpecialName].NodeValue;
end;
node.AttributeNodes.Nodes[XMLAttrName].NodeValue:=value;
end;
xmlDocument.SaveToFile(strEntityEngineFile);
end;
end;
result:=true;
except
result:=false;
end;
xmlDocument.Active:=false;
end;
<script type="text/javascript"><!--google_ad_client = "pub-3528650120430763";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text_image";google_ad_channel = "";google_ui_features = "rc:0";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3528650120430763&dt=1183799909421&lmt=1183799909&format=468x60_as&output=html&correlator=1183799909421&url=http%3A%2F%2Fpages.google.com%2Fdraft%2FGmai9999%2Fhome%3Fauthtoken%3D397572419ac6db50f5ab16961d0b13491bee0967&ad_type=text_image&ref=http%3A%2F%2Fpages.google.com%2Fpreview%2FGmai9999%2Fhome%3Fauthtoken%3D9686509b5e94c7e953ceecc7a041fbc446802a9a%26no-cache%3D15040874706351293&ui=rc%3A0&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="468" scrolling="no" height="60" allowtransparency="allowtransparency"></iframe>
- Delphi直接读写XML修改版
- Delphi直接读写XML修改版
- Delphi直接读取XmL
- delphi读写XML
- Delphi xml 读写
- DELPHI读写xml文件
- 直接修改storyboard xml
- ZT:Delphi直接读取XmL
- JAVA xml读写修改
- Delphi 读写XML文件方式
- XML学习笔记(三)--Delphi读写XML
- XML学习笔记(四)--Delphi读写xml
- Delphi读写XML的两个函数
- XML学习笔记(五)--Delphi读写xml例子
- VB scrpit 修改配置文件(读写XML)
- java读写和修改XML文件
- dom4j xml读写修改等操作
- XML 读写 怎么修改web.config
- 美容
- JUnit入门
- ajax
- 软件过程改进步骤原则
- wxWidgets点滴(1)- 注意wxImageList的取出效率
- Delphi直接读写XML修改版
- 纪念
- 使用 FancyUpload需要注意的一点
- 昨天又病了:(
- 架构讲义1-2
- 理财方案:工薪一族月薪2 000元的理财计划
- 搬家了
- Delphi 操作xml文件入门资料
- Solaris下的设备和设备文件