Usage of the XML parsing control txmldocument in Delphi

Source: Internet
Author: User

Txmldocument control usage in Delphi


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 <? Xmlversion = "1.0" encoding = "gb2312"?> <! -- Document version information, in the same format as HTML -->

2 <xmlpackage>

3 <clinetinfoip = "202.101.100.90" handler = "Si" unit = "Jiangsu network provider software"/>

4 <DATA>

5 <rowid = "1" color: red; font-family:; MSO-ascii-font-family: 'timesnewroman '; MSO-Hansi-font-family: 'timesnewroman '"> sun" Sex = "male" age = "24" duty = "software engineer"/>


6 <rowid = "2" color: red; font-family:; MSO-ascii-font-family: 'timesnewroman '; MSO-Hansi-font-family: 'timesnewroman '"> 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 <memolength = "16" color = "$ 0034494b"> hello! Iamyaya! </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 <actionsacition = "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 xmldatabindingwizard provided by the control to create an interface unit suitable for the user. Then, use the interfaces provided by the interface unit to read and write similar XML files, which is very convenient, but there are 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: file askxmllab_xmlcountry.xml 'of work Aya ');


You can also create them dynamically as follows:


XML: = txmldocument. Create (NiL );


XML. loadfromfile ('f: The file askxmllab_xmlcountry.xml of work fila ');


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: The file askxmllab_xmlcountry.xml of work AyA;


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: The file askxmllab_xmlcountry.xml of work fila ');


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:

Varrootnode: 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:


Varnode: 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! Iamyaya! ', 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: The file askxmllab_xmlest.xml of work fila ');


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 change the add (constnode: ixmlnode) method above to insert (Index: integer; constnode: 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:


Varlen: 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:


Nodetype


Nodevalue


Ntattribute Node


Attribute Value


Ntelement Element Node


If the node does not have a subnode, the text is returned. Otherwise, an exception occurs.


Nttext Node


Text Content


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:


Nodetype


Nodename


Ntattribute Node


Attribute name


Ntelement Element Node


Node name


Nttext Node


'# Text'


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!

 


Ii. Control parsing method:


Select an XML template file for the filename attribute of the control, and then select xmldatabindingwizard… by pressing the control ..., An XDB file will be created in the middle of this wizard. In the pop-up Wizard, on the first page, you can see that Delphi has helped you map nodes in the XML file to the interface. Here you can expand each node and edit the attribute data type of each node. Click next to the next configuration page. Here you can see that Delphi generates the Framework Code for each node interface. Click finsh to complete the Wizard. In this way, Delphi will generate an XDB file and the XML interface unit corresponding to the XML document structure. When you access this type of XML file later, you can easily read the node by referring to the methods and attributes in the generated XML interface unit:

Varxml: ixmlxmlpackagetype; // The Root Interface generated by the wizard

Begin

XML: = loadxmlpacage (''f: Work object askxmllab_xmlest.xml '); // This method is also in the generated interface unit.

XML. Data. Row [1]. Name; // For example, if you want to obtain the first

Usage of the XML parsing control txmldocument in Delphi

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.