Sometimes, just use XML to make some small applications, such as logs, or simply to save some configuration, then we only need to directly read and write XML like, efficiency first.
Delphi box has a XML file directly read and write (and code examples), the core function of the following two functions (a read-write):
{ ------------------------------------------------- ------------------------------ Fun / Pro: GetXMLNodeValue @date: 2004.12.11 @Param: xmlFile xml file @Param: xmlnodepath node @Param: xmlattrname node attribute name, if direct access to the node value of this parameter may be ignored. @Param: Parameter node dep delimiter defaults. @Return: a first node value --------------------------- -------------------------------------------------- - } function GetXMLNodeValue (strEntityEngineFile: String; xmlNodePath: String; const xmlattrname: String = '' ; const DEP: Char = ' . ' ): String; var the 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 ]) the then the begin // scan node for I: = . 1 to urlcount- . 1 do the begin IF (Node <> nil ) the then Node: = getnodefromIXMLNodeList (node.childNodes, XmlNodeList [I]) the else BREAK; End ; IF (node = nil ) the then the begin Result: = '' ; End the else the begin // determination is to take the contents attribute or the access node 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 node attribute name, if direct access to the node value of this parameter may be ignored. @Param: Parameter node dep delimiter defaults. @Return: No operation is successful ------------------------------ ------------------------------------------------- } function setXmlNodeValue (strEntityEngineFile: String; xmlNodePath: String; const xmlattrname: String = '' ; const value: String = '' ; const DEP: Char = ' . ' ): Boolean; var the xmlDocument: IXMLDocument; Node: IXMLNode; XmlNodeList: of TStrings; I: Integer; URLCOUNT: Integer; the begin // XML node path 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; the except Result: = to false; End ; xmlDocument.Active: = to false; End ; However, these two functions there is a problem: it can only find the first record by the node and attribute names. For example: the node to be the same if the operation is similar to the following XML files and a plurality of attribute names, attribute values but not the same as the above read function will strike. <Color name = "Normal attribute" Red = " 100 " Green = " 125 " Blue = " 150 " /> <Color name = "Good attribute" Red = " 150 " Green = " 175 " Blue = " 200 is " /> <Color name = "attribute Excellent" Red = " 0 " Green = " 0 " Blue = " 255 " /> OK, the greatest pleasure is to do it yourself programmers. Let's transform the look of these two functions. On the basis of the original function adds two parameters: function TOperateXml.getnodefromIXMLNodeList(childnodes: IXMLNodeList; nodename: string): IXMLNode; var i: Integer; begin for i := 1 to childnodes.Count do begin if (childnodes.Get(i - 1).NodeName = nodename) then begin result := childnodes[i - 1]; exit; end; end; end; {------------------------------------------------------------------------------- Fun/Pro: GetXMLNodeSpecialValue @Date: 2004.12.11 @Param: xmlFile xml file @Param: xmlnodepath node @Param: xmlattrname node property names, if direct access node values you can omit this parameter. @Param: XMLSpecialName node attribute name to find @Param: an attribute of the node corresponding to find XMLSpecialValue value @Param: dep node parameter delimiter default. @Return: the value of a property ---- -------------------------------------------------- ------------------------- } 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 the else the begin // determination is to take the contents attribute or the access node IF (Trim (xmlattrname) = '' ) the then Result: = Node.text the else the begin Result: = node.AttributeNodes.Nodes [XMLSpecialName] .NodeValue; // here do not want to declare a temporary variable, and to compare it with the result, there may be hidden dangers. the while ((Result <> XMLSpecialValue)) do the begin Node: = node.NextSibling; the while (node.NodeName = ' #comment ' ) do the 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 file @Param: xmlnodepath node @Param: xmlattrname node property names, if direct access node values you can omit this parameter. @Param: XMLSpecialName node attribute name to find @Param: an attribute of the node corresponding to find XMLSpecialValue value @Param: dep node parameter delimiter default. @Return: the success of the operation ---- -------------------------------------------------- ------------------------- } 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;