Originally, I used the txmldocument class that comes with Delphi to read and write XML files. Although it is troublesome, It is not bad in terms of functions. There is only one thing that makes me really uncomfortable-every time I create a txmldocument class instance, I must input a tcomponent object. This means that if I encapsulate the read and write of XML files into a class, the tcomponent object must also be input when the custom class is created.
I tried many methods, but they could not be avoided. I finally tried to find the nativexml library online.
After the download, I immediately opened the demo and looked at it. Cool. When creating tnativexml, I only needed to input the XML file path. Then I was pleasantly surprised when I looked down. It has encapsulated most operations and can serialize any object.
For example, you can store the entire form into an XML file through the tsdxmlobjectwriter = Class (tpersistent) class and read it again next time. This makes it easy to remotely transmit objects.
The following is an example of using the nativexml Library:
Target XML structure:
<Bookshift>
<Book author = "test_author">
<Date> 2000-01-01 </date>
</Book>
</Bookshift>
Delphi code:
Procedure writetest;
VaR
XML: tnativexml;
N_bs: txmlnode;
Begin
// Create a root node
XML: = tnativexml. createname ('bookshift ');
XML. encodingstring: = 'gb2312 ';
// The output style is readable.
XML. xmlformat: = xfreadable;
// Create a book Node
N_bs: = xml. Root. nodenew ('book ');
// Write the attributes of the book Node
N_bs.writeattribuitestring ('author', 'test _ author ');
// Create the date node of the book node and write the value
N_bs.writestring ('date', '2017-01-01 ');
XML. savetofile ('test. xml ');
End;
Open the test. xml file and check the format above.
It's easy. I used this library to rewrite the original XML Parser class, saving about 40% of the Code.