Delphi XML read/write

Source: Internet
Author: User
Tags count trim

XML syntax schema 1, ignoring whitespace
2. Comment: <!---->
3, directive: <??>
4. Special characters: < > ' & Overrides: &lt; &gt; &apos; &quot; &amp;
5. Binary data: <! [CDATA [...]] >
6. Case-sensitive
7, mark can not overlap
8, single mark, that is, empty elements, such as: <node/>, only attributes are used to record data
9. Attribute values within quotation marks
10. The name of the element and attribute in the XML must begin with a letter or _, followed by a letter, a number, or _-:. , but you cannot start with XML.
11, the file header contains version and character encoding,
such as: <?xml version= "1.0" encoding= "UTF-8"?>, <?xml version= "1.0" encoding= "gb2312"?>
1, Txmldocument for in-memory operations and building new XML
2, a txmldocument document is actually a node collection
3, each node includes name, text content, attribute collection, child node collection
4, you can access nodes by location or by name
5, each node is a Ixmlnode object
6, the set of nodes contained under each node is a Ixmlnodelist object that gets the ChildNodes property of the node
7, the root node xmldocument1.documentelement is also a Ixmlnode object
8, XMLDocument1.DocumentElement.ChildNodes is a Ixmlnodelist object, and access generally starts here
9, you can think of attributes as horizontal nodes
Read XML nodes (versions that have been optimized and can be used directly by copy)
{-------------------------------------------------------------------------------
Fun/pro:getxmlnodespecialvalue
@Date: 2004.12.11
@Param: xmlfile XML file
@Param: Xmlnodepath Node
@Param: The name of the property in the Xmlattrname node, which can be ignored if the node value is directly taken.
@Param: Xmlspecialname The name of the attribute in the node to find
@Param: Xmlspecialvalue The value of an attribute in the node to find
@Param: The delimiter for the parameters of the DEP node, which defaults to.
@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 node path
Xmlnodelist:=tstringlist.create;
XMLNODELIST.DELIMITER:=DEP;
Xmlnodelist.delimitedtext:=xmlnodepath;
Urlcount:=xmlnodelist.count;
XML Object
XmlDocument: =txmldocument.create (nil);
Xmldocument.loadfromfile (Strentityenginefile);
Xmldocument.active:=true;
Try
Node:= xmldocument.documentelement;
if (node. NodeName = xmlnodelist[0]) THEN BEGIN
Scan node
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
Determine whether to take a property or to take a node content
if (Trim (xmlattrname) = ") Then
Result:=node. Text
Else
Begin
Result: = node. Attributenodes.nodes[xmlspecialname]. NodeValue; Here you do not want to declare a temporary variable, using result to compare, there may be hidden trouble.
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
Write function (already optimized version, can be used directly by copy)

{-------------------------------------------------------------------------------
Fun/pro:setxmlnodespecialvalue
@Date: 2004.12.11
@Param: xmlfile XML file
@Param: Xmlnodepath Node
@Param: The name of the property in the Xmlattrname node, which can be ignored if the node value is directly taken.
@Param: Xmlspecialname The name of the attribute in the node to find
@Param: Xmlspecialvalue The value of an attribute in the node to find
@Param: The delimiter for the parameters of the DEP node, which defaults to.
@Return: Operation succeeded or not
-------------------------------------------------------------------------------}
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 node path
Xmlnodelist:=tstringlist.create;
XMLNODELIST.DELIMITER:=DEP;
Xmlnodelist.delimitedtext:=xmlnodepath;
Urlcount:=xmlnodelist.count;
XML Object
XmlDocument: =txmldocument.create (nil);
Xmldocument.loadfromfile (Strentityenginefile);
Xmldocument.active:=true;
Try
Node:= xmldocument.documentelement;
if (node. NodeName = xmlnodelist[0]) THEN BEGIN
Scan node
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


Many users on the net reflect getnodefromixmlnodelist this function is not declared, now the function is pasted out. As a supplement to the above posts

function 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.