Application of XML document in Delphi6 / 7
XML documents are a new generation of web data format. It can store all data in the form of a tree.
Let's introduce some of the usage of the TXMLDocument control:
Defined:
Xmldoc: txmldocument;
Encodermime: TIDENCODERMIME;
Decodermime: Tiddecodermime;
XN: IXMLNode;
f: tfilestream;
Encodermime and Decodermime are MIME tools in the INDY control to convert all types of strings to MIME (Base64) ASCII code, you can use other encoding methods, such as UUE, XXE.
XN is a tree knot of an XML document. F is the file stream.
First, load an XML document
XMLDoc.LoadFromFile ('XML document file name ");
XMLDoc.active: = true; // makes XML documents become active
Again, positioning the location of XN in the XML document (assuming the fourth node of the second layer)
XN: = xmldoc.node;
XN: = xn.childNodes.nodes [1]; // node number is from zero, so it is necessary to reduce one
XN: = xn.childNodes.nodes [3];
then,
Add the next level node
XN: = xn.addchild ('new node name ")
Xn.Text: = 'The text content of the node'
Xn.attributes [first attribute "]: = 'attribute value'
Xn.attributes [second attribute "of the node ']: =' attribute value
Modify the node, (the name of the node cannot be changed)
Xn.text: = 'New Text Content'
Xn.attributes [second attribute ']: =' new attribute value '
Delete node XN
Xn.ParentNode.childNodes.delete (xn.parentnode.childNodes.indexof (XN));
Use XN.Text to store files,
f: = TFileStream.create ('The file name to be stored ", FMOPENREAD);
Xn.text: = Encodermime.Encode (f); // Conversion format, file code
f.free;
Decrect files in xn.text,
f: = TFileStream.create ('target file name', fmcreate);
Decodermime.Decodetostream (xn.text, f);
f.free;
It is best to use xn.attribute to add an attribute to represent the encoding method of the file, such as
Xn.attributes ['Type'] = 'Base64'
In this way, the two pieces of the above are:
Xn.attributes ['Type']: = 'base64';
f: = TFileStream.create ('The file name to be stored ", FMOPENREAD);
Xn.Text: = Encodermime.Encode (f);
f.free;
IF xn.istextelement // xn exists in TEXT
Then if xn.hasettribute ('type') THEN // XN exists in attribute 'type'
if xn.attributes ['Type'] = 'Base64' Then
Begin
f: = TFileStream.create ('target file name', fmcreate);
Decodermime.Decodetostream (xn.text, f); f.free;
END;
Finally, give all tree structures using TtreeView to display XML documents.
XMLTree is a TTREEVIEW class
Procedure TFORM1.Readxmltree (Roottreenode: Ttreenode; XMLTREENODE: IXMLNODE);
VAR i: integer; c: ttreenode;
Begin
For i: = 0 to xmltreenode.childnodes.count-1 do
Begin
C: = xmltree.Items.addchild (roottreenode, xmltreenode.childnodes.nodes [i] .nodeename);
Readxmltree (C, Xmltreenode.childNodes.nodes [i]);
END;
END;
Instructions
Readxmltree (nil, xmldoc.node);
Of course, load XML document J ^ _ ^
As for the use of XML documents as the In-Memory database, use the Delphi6 / 7 XML maping Tool, first create a simplex XML document, then generate the .xtr file with mappingTool, use TXMLDataSetProvider as the data provider, TDataSet connection, TTable can use XML as a database, detailed, see Internet / XMLTransFormProvider under DELPHI6 / 7 DEMOS directory!
------------------------------------------- Power by liu yang 2002-2 -8
/ by ly http://lysoft.7u7.net or http://liuyang.7i24.com
An example of using XML: Site Manager file in LY FTP Explorer
Can be downloaded from my homepage.