Txmldocument control usage in Delphi

Source: Internet
Author: User
Tags xml attribute

The parsing of XML files in Delphi is much easier than directly using the interfaces in MS msxml2_tlb.
An XML instance is provided before the lecture. In some cases, it is easier to understand the XML instance by combining the instance.
1 <? XML version = "1.0" encoding = "gb2312"?> <! -- Document version information, in the same format as HTML -->
2 <xmlpackage>
3 <clinetinfo IP = "202.101.100.90" handler = "Si" unit = "Jiangsu network provider software"/>
4 <DATA>
5 <row id = "1" name = "sun" Sex = "male" age = "24" duty = "software engineer"/>
6 <row id = "2" name = "moon" Sex = "female" age = "25" duty = "department manager"/>
7 <! -- Data contains two instances of the element row, which are differentiated by attribute IDs -->
8 </data>
9 <memo length = "16" color = "$ 0034494b"> hello! I am Yaya! </Memo> <! -- The content contained in this element becomes text, and it also contains two attributes: length and color. Of course, there can be no attribute -->
10 <actions acition = "update/Insert"/> <! -This element describes an action control information -->
11 </xmlpackage>
The txmldocument control provides two common methods: 1. Using the attributes and Methods encapsulated in this class to read and write XML files, I call it my own parsing method; 2. Use the XML data binding Wizard provided by the control to create an interface unit suitable for the user. Then, follow the interfaces provided by this interface unit to read and write similar XML files, it is very convenient, but it has limitations. I call it the control parsing method. I will focus on the first method.
I. Self-Resolution:
The following describes three actions: Create, read, modify, add, and delete.
(1) Create and add:
First, you need to create an XML instance, which can be dynamically created as follows:
Txmldocument. Create ('f:/work/Faya file/task/XML/lab_xml/country. xml ');
You can also create them dynamically as follows:
XML: = txmldocument. Create (NiL );
XML. loadfromfile ('f:/work/Faya file/task/XML/lab_xml/country. xml ');
Of course, you can drag and drop a txmldocument control from the Internet panel and assign values to the filename or XML Attribute. For example, the object name is still named XML (not explicitly mentioned below ):
XML. filename: = F:/work/Faya file/task/XML/lab_xml/country. xml;
As for its other attribute, XML is used to directly assign the XML language, this usage is not important for us at the moment. After creating an XML instance, you must enable the active attribute:
XML. Active: = true;
In this way, the file read and write operations are effective. If there is no XML file available for creating an XML instance, that is, to create an XML file, you can choose to operate on the XML instance after the preceding sentence. After the operation is completed, add:
XML. savetofile ('f:/work/Faya file/task/XML/lab_xml/country. xml ');
The following describes how to create a node based on the preceding XML file instance. First, an XML file can only have one root node. For example, the xmlpackage of two rows in the file instance is created as follows:
VaR rootnode: xmlnode;
Rootnode: = xml. createnode ('xmlpackage ');
XML. documentelement: = rootnode;
Next, we create different types of subnodes. the nodes in the first row of the instance are representative. It is a subnode with text and two attribute nodes, let's see how it was created. First, create a root node:
XML. Active: = true;
XML. documentelement: = xml. createnode (''' xmlpacage '''');
Then we create a memo subnode:
VaR node: ixmlnode;
Node: = xml. createnode ('''memo '''');
XML. documentelement. childnodes. Add (node );
This method is better for creating a son node. Of course, you can:
XML. documentelement. addchild (''' xmlpacage '''');
We recommend that you use the first method. The createnode method is used in a wide range of ways. The second parameter is mainly used. The default parameter is used in the preceding method, next let's take a look at how to create the text of this subnode:
Node. childnodes. Add (XML. createnode (''' hello! I am Yaya! ''', Nttext ));
Note the second parameter of the createnode method. Let's continue to see how the attribute is created:
Node. attributenodes. Add (XML. createnode (''' length ''', ntattribute ));
Node. setattribute (''' length ''', 16 );
Node. attributenodes. Add (XML. createnode (''' color ''', ntattribute ));
Node. setattribute (''' color ''', $ 0034494b );
Do not forget to save it:
XML. savetofile (''' F:/work/Faya file/task/XML/lab_xml/test. xml '''');
XML. Active: = false;
At this point, we end the creation. As for adding, that is, inserting a node, We need to insert it into the specified node and replace the add (const node: ixmlnode) method above with insert (Index: integer; const node: ixmlnode.
(2) read and modify
Reading is relatively simple, mainly to read the text and attribute values of the subnode, the method is relatively simple, the key is to use the ixmlnodelist interface to provide some attributes and methods to search for a specified node cyclically. There are many ways to read text and attribute values in Delphi. I will talk about the nodevalue attribute, which is an attribute of the ixmlnode interface and its value is of the olevariant type, this means that although we all store strings in XML files, Delphi will help us to convert the data format. For example, there is an attribute node called node:
Node. nodevalue: = 16; // 16 is of the integer type.
Although the XML file stores 16 in the ASCII format, but when we want to read it, Delphi will help us change it back, as long as we can:
VaR Len: integer;
Len: = node. nodevalue;
Of course, we should note that we should first check the nodetype attribute of the same node when reading this method, because there are several types of nodes to avoid exceptions, as shown in the following comparison:
The above is about the value of the read and write nodes. Sometimes you need to read and write the node name and access the nodename attribute. The usage is roughly the same. It should also be used with nodevalue:
The read is complete. Of course, the above is done through the interface attributes, and the interface also has methods to complete, so we will not mention it. The above attributes are readable and writable, therefore, the changes are basically clear.
(3) Delete
The delete operation mainly describes how to delete a specified node. It mainly describes several methods of the ixmlnodelist interface, including the clear and delete methods. The first method is to clear all, and the last one is to delete the specified subnode, this method is overloaded. you can press the name or sequence number, that is, the Count attribute, which is very convenient!

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.