Sometimes, just do some small applications with XML, such as simply saving logs or some configuration, then we only need to read and write XML directly, efficiency first.
Delphi box has a box
Read and write XML files (examples and code) directly, its core function is the following two functions (one read and written):
{------------------------------------- ------------------------------ fun / pro: getXmlnodevalue @Date: 2004.12.11 @Param: XMLFile XML file @Param: XMLNodePath Node @Param: The attribute name in the XMlattrName node can ignore this parameter if the node value is directly taken. @Param: The separator of the parameter of the DEP node, defaults. @Return: the value of the first node --------------------------- -------------------------------------------------- -}
Function getXmlnodeValue (String; XMLNodePath: String;
Const
XMlattrname: 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 (strlDocument.act); xmlDocument.active:
=
True
;
Try
Node:
=
XmlDocument.documentelement;
IF
(node.nodeename
=
XMLNodelist [
0
]) The begin
//
Scan node
for
i:
=
1
To urlcount
-
1
DO
Begin
IF
(Node)
<>
NIL) THEN NODE:
=
GetNodeFromixmlNodelist (Node.childNodes, XMLNodelist [i])
Else
Break;
IF
(Node)
=
NIL) THEN BEGIN RESULT:
=
'' '
; END
Else
Begin
//
Judgment is to take the property or take the node content
IF
(TRIM (XMlattrName)
=
'' '
Then result: =
Node.text
Else
RESULT:
=
Node.attributenodes.nodes [xmlattrname] .NodeValue; end;
Else
Begin Result:
=
'' '
End; Except Result:
=
'
Error
'
; End; xmlDocument.active:
=
False
;
{------------------------------------- ------------------------------ FuN / pro: setXmlnodevalue @Date: 2004.12.11 @Param: XMLFile XML file @Param: XMLNodePath Node @Param: The attribute name in the XMlattrName node can ignore this parameter if the node value is directly taken. @Param: The separator of the parameter of the DEP node, defaults. @Return: Do not operate success -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------}
Function setXMLNodeValue (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 node path
XMLNodelist:
=
Tstringlist.create; xmlnodelist.delimiter:
=
DEP; XMLNodelist.delimitedText:
=
XMLNodePath; Urlcount:
=
XMLNodelist.count;
//
XML object
XMLDocument:
=
TXMLDocument.create (nil); Xmldocument.LoadFromFile (strlDocument.act); xmlDocument.active:
=
True
;
Try
Node:
=
XmlDocument.documentelement;
IF
(node.nodeename
=
XMLNodelist [
0
]) The begin
//
Scan node
for
i:
=
1
To urlcount
-
1
DO
Begin
IF
(Node)
<>
NIL) THEN NODE:
=
GetNodeFromixmlNodelist (Node.childNodes, XMLNodelist [i])
Else
Break;
IF
(Node)
<>
NIL) THEN Begin
IF
(TRIM (XMlattrName)
=
'' '
Then Node.Text:
=
Value
Else
Node.attributenodes.nodes [xmlattrname] .NodeValue:
=
Value; xmlDocument.savetofile (strentral); end; end; result:
=
True
EXCEPT RESULT:
=
False
; End; xmlDocument.active:
=
False
;
However, the above two functions have a problem: it can only find the first record in the node name and attribute name. Example: If you want to operate similar to the following XML files, nodes, and attribute names, only the value of the attribute is different, the above read and write function strikes.
<
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"
/>
OK, the biggest fun of programmers is to do it. Let's transform these two functions.
Two parameters are added based on the original function:
Read function:
{------------------------------------- ------------------------------ Fun / Pro: getXmlnodespecialvalue @date: 2004.12.11 @Param: XMLFile XML file @Param: XMLNodePath Node @Param: The attribute name in the XMlattrName node can ignore this parameter if the node value is directly taken.
@Param: Xmlspecialname To find the property name @Param: XmlspeCialValue to find a value of a value corresponding to a value @Param: DEP node's parameter, default. @Return: Value of a property ----- -------------------------------------------------- -------------------------} Function getXmlnodespecialValue (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 objects xmlDocument: = TXMLDocument.Create (nil); xmlDocument.LoadFromFile (strEntityEngineFile); xmlDocument .Act: = true; try node: = xmldocument.documentelement; if (node.nodeename = xmlnodelist [0]) THEN BEGIN / / Scan Node f OR i: = 1 to urlcount-1 do begin if (Node <> nil) thrnode: = getnodefromixmlnodelist (node.childnodes, xmlnodelist [i]); Else Break; end; if (node = nil) THEN BEGIN RESULT : = '; END ELSE Begin // Judgment is to take the attribute or take the node content IF (Trim (XmlattrName) =' ') Then Result: = node.text else begin Result: = node.attributenodes.nodes [xmlspecialname] .NodeValue ;
/ / I don't want to declare a temporary variable again, and use results to compare, there may be hidden dangers. 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 beg; end; eXCept result: = '; end; xmlDocument.active: = False; End; write function:
{------------------------------------- ------------------------------ Fun / Pro: setXmlnodespecialvalue @date: 2004.12.11 @Param: XMLFile XML file @Param: XMLNodePath Node @Param: The attribute name in the XMlattrName node can ignore this parameter if the node value is directly taken. @Param: Xmlspecialname To find the node @Param: XmlspeCialValue to find a value of a value in the node @Param: DEP node's segment of the parameter of the parameter, default. @Return: Successful operation is ---- -------------------------------------------------- -------------------------}
Function setXMLNODESPECIALVALUE (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 (strlDocument.act); xmlDocument.active:
=
True
;
Try
Node:
=
XmlDocument.documentelement;
IF
(node.nodeename
=
XMLNodelist [
0
]) The begin
//
Scan node
for
i:
=
1
To urlcount
-
1
DO
Begin
IF
(Node)
<>
NIL) THEN NODE:
=
GetNodeFromixmlNodelist (Node.childNodes, XMLNodelist [i])
Else
Break;
IF
(Node)
<>
NIL) THEN Begin
{IF (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.nodeename
=
'
#comment
'
)
DO
Begin Node:
=
Node.nextsibling; end; cmpvalue:
=
Node.attributenodes.nodes [xmlspecialname] .NodeValue; end; node.attributenodes.nodes [xmlattrname] .NodeValue:
=
Value; end; xmlDocument.savetofile (strentral); end; end; result: =
True
EXCEPT RESULT:
=
False
; End; xmlDocument.active:
=
False
;