Three methods for creating an ixmldomdocument object:
(1) [method 1] directly create ixmldomdocument, for example (1)
Uses MSXML;
VaR DOC: ixmldomdocument;
DOC: = codomdocument. Create;
.....
DOC: = nil;
A) Doc. Load ('C: \ Temp. xml'); // load data from a file
B) Dynamic Creation
VaR aelement, aelement2: ixmldomelement; // [anode: ixmldomnode ==>. appendchild ()]
// Add version information '<? XML version = "1.0"?> '
Doc. appendchild (Doc. createprocessinginstruction ('xml', 'version = "1.0" encoding = "gb2312 "'));
(*) Because the returned results of this function do not contain 'encoding = "gb2312" ', you must note before saving.
// Add the root node
Doc. appendchild (Doc. createelement ('bootdocnode'); // = aelement
// Add a subnode
Aelement: = ixmldomelement (Doc. documentelement. appendchild (Doc. createelement ('chilenode1 ')));
// Set the contact property
Aelement. setattribute ('id', '11 ');
Aelement. setattribute ('Units ', 'yuan/m2 ');
// Set the node content
Aelement. appendchild (Doc. createtextnode ('node content '));
// Add a child node
Aelement2: = ixmldomelement (aelement. appendchild (Doc. createelement ('child _ chilenode1 ')));
(2) [method 2] directly create ixmldocument (not ixmldomdocument)
Uses xmlintf, xmldoc;
VaR xmldoc: ixmldocument; anode: ixmlnode; s: string;
Xmldoc: = txmldocument. Create (NiL );
Try
// Add version information '<? XML version = "1.0" encoding = "gb2312"?> '
Xmldoc. Active: = true; xmldoc. Version: = '1. 0'; xmldoc. encoding: = 'gb2312 ';
// Add the root node
Anode: = xmldoc. addchild ('bootdocnode ');
// Add a subnode
Anode: = anode. addchild ('chilenode1 ');
// Set the contact property
Anode. setattribute ('id', '22 ');
Anode. setattribute ('Units ', 'yuan/m2 ');
// Set the node content
Anode. Text: = 'node content ';
// Add a child node
Anode: = anode. addchild ('child _ chilenode1 ');
Anode. Text: = 'child _ chilenod content ';
S: = xmldoc. xml. Text; // tstrings is returned in. xml.
Finally
Xmldoc: = nil;
End;
(3) Use xmldatabinding
I) Prepare an XML file, which is representative and ensures that all nodes and their relationships in the program exist.
Ii) use file --> New --> XML Data Binding
Iii) Create an XML Object
A) V: string; // XML file content
DOC: ixmlbudgetdoctyp; // ixmlbudgetdoctyp is the root node of the XML file.
DOC: = loadxmldata (V). getdocbinding ('budgetdoc', txmlbudgetdoctype) as ixmlbudgetdoctype;
B) Doc: = loadbudgetdoc ('C: \ Temp. xml ');
Iv) Application
Doc. childnodes. findnode ('docfile') as ixmldocfiletype;
(4) use the txmldocument Control
Xmldocument1.filename: = 'C: \ Temp. xml ';
Xmldocument1.active: = true; // xmldocument1 is equivalent to Doc
(5) Other methods of XML objects
Ixmlnodelist. findnode (nodename: domstring): ixmlnode;
Ixmlnodelist. findnode (nodename, namespaceuri: domstring): ixmlnode;
Ixmlnodelist. findnode (childnodetype: tguid): ixmlnode;
Ixmlnodelist. findsibling (const node: ixmlnode; Delta: integer): ixmlnode;
Ixmlnodelist. First: ixmlnode;
Ixmlnodelist. Last: ixmlnode;
......
//////////////////////////////////////// ////////////////////////////////
// Example (1)
Uses MSXML;
DOC: ixmldomdocument;
Budgetdoc: ixmldomnode;
Rela: ixmldomnode;
RS: ixmldomnodelist;
// Create or retrieve an XML Node
DOC: = codomdocument. Create;
Doc. Load ('C: \ Temp. xml ');
Budgetdoc: = Doc. selectsinglenode ('budgetdoc ');
Rela: = budgetdoc. selectsinglenode ('relation ');
// Create an XML subnode
If not assigned (RelA) then
Begin
Rela: = Doc. createelement ('relation ');
Rela. setattribute ('budgetid', 0 );
Rela. setattribute ('name', 'name ');
Budgetdoc. appendchild (RelA );
End;
// Obtain the subnode (sequence)
RS: = rela. selectnodes ('rela [@ budgetid = "2" and @ taskid = "8"] ');
For I: = 0 to Rs. Length-1 do
Begin
S: = S + Rs [I]. Attributes. getnameditem ('newrate'). nodevalue;
End;
Rela: = Doc. childnodes. findnode ('domainnode') as ixmldomnode;
// Remove the child node
For I: = Rs. Length-1 downto 1 do
Rela. removechild (RS [I])
// Obtain the parent node
If assigned (anode. parentnode) and (anode. parentnode. nodename = 'task') then
Result: = anode. parentnode;
// Get attributes
Doc. documentelement. attributes ['name']
Rela. attributes ['name']
Rs. nodes [I]. attributes ['name']
Ajob: ixmldomnode; jobs: ixmldomnodelist;
Jobs: = ajob. selectnodes ('rcj [@ attrib = "'+ aattrib +'"] ')