XML parsing control TXMLDocument in Delphi

xiaoxiao2021-03-06  66

Usage of TXMLDocument control in Delphi

Delphi is very good to the XML file, which is more convenient than the interface in MSXML2_TLB directly using MS, is now called below.

A XML instance is given before telling, and some parts are readily understood.

1

2

3

Handler = "si" unit = "Jiangsu Network Business Software" />

4

5

Name = "Sun" SEX = "Men" AGE = "24" Duty = "Software Engineer" />

6

Name = "Moon" SEX = "Female" Age = "25" Duty = "Department Manager" />

7

8

9

Color = "$ 0034494B"> Hello! I am Yaya!

10

11

The TXMLDocument control provides two common practices: 1. Directly use this type of package to read and write XML files, I call it its own parsing method; Second, use the XML Data Binding Wizard provided by the control to create a suitable User's own interface unit, followed by many of the interfaces provided by this interface unit to achieve reading and writing of the same type XML file, is very convenient, but there is limitations, I call it a control parsing method. I focus on the first method of use.

First, analyze the law:

Below I will follow the five actions in creating, reading, modifying, adding, and deleting these five actions.

(1) Create and add:

First create an XML instance, you can create this:

TXMLDocument.create ('f: / Work / Fah file / task/xml/LAB_XML/COUNTRY.XML');

It can also be created dynamically:

XML: = TXMLDocument.create (nil);

XML. LoadFromFile ('f: / Work / Fah file / Task/xml/LAB_XML/COUNTRY.XML');

Of course, you can also drag and drop a TXMLDocument control from the Internet panel, and then assign values ​​for the filename or XML attribute, such as the object name is still called XML (seeing this default):

Xml.FileName: = f: / Work / Feria file / Task/xml/LAB_XML/COUNTRY.XML;

As for another attribute XML, it is used to directly assign XML language, which is not important to us. Be careful to open the Active property after creating an XML instance:

XML.Active: = true; this is valid for the reading and writing of the file below. If no XML file can be used to create an XML instance, you have to build an XML file, you can choose to operate on the XML instance after the operation, plus:

Xml.savetofile ('f: / Work / Feria file / task/xml/LAB_XML/COUNTRY.XML');

Here is to create a node, you need to talk about the above XML file instance. First, an XML file can only have one root node, such as the XMLPackage of 2 lines in the file instance is created:

Var rootnode: XmlNode;

Rootnode: = Xml.createnode ('XMLPackage');

XML. DocumentElement: = rootnode;

Then we create different types of child nodes, and the nodes of the nodes in the first example in the previous instance are relatively representative. It is a child node with text, and there are two attribute nodes, let's see how it is. Created, first create a root node:

XML.Active: = true;

Xml.documentelement: = Xml.createnode ('Xmlpacage');

Then we create a MEMO child node:

Var node: IXMLNode;

Node: = Xml.createnode ('MEMO');

Xml.documentelement.childNodes.Add (Node);

This method of creating son nodes is better, of course,:

Xml.Documentelement.addchild ('Xmlpacage');

To create, it is recommended to use the first one. The use of the CreateNode method is very rich. It is mainly to see its second parameters. It is actually used by default parameters. Here I look at how to create this child's text:

Node.childNodes.Add (Xml.createnode ('Hello! I am Yaya!', NTTEXT));

Note that the second parameter of the CreateNode method, we continue to see how the properties are created:

Node.attributenodes.add (Xml.createnode ('Length', NTATTRIBUTE);

Node.setttribute ('Length', 16);

Node.attributenodes.add (Xml.createnode ('Color', NTATTRIBUTE));

Node.setttribute ('Color', $ 0034494B);

Don't forget to save:

Xml.savetofile ('f: / work / feet file / task/xml/LAB_XML/TEST.XML');

XML.Active: = false;

At this end, we ended the creation, as for adding, inserting nodes, replacing the above add (const node: ixode: ixmlnode) after the specified node is specified, and other usage is also available. At a glance, it is no longer detailed.

(2) Read and modify

Read is relatively simple, mainly reading the text and attribute values ​​of the child node, the method is relatively simple, the key is to use the IXMLNodeList interface to provide some attributes and methods to loop to the specified node OK. The method of the text and attribute values ​​is available in the method provided in Delphi. I will talk about: NodeValue properties, it is an attribute of the IXMLNode interface, its value is the Olevariant type, which means that we are in the XML file. It is a string, but Delphi will help us to convert data format, such as a property node called node: node.nodevalue: = 16; // 16 is Integer type

Although saved in the XML file is 16 of ASCII format, but when we want to read, Delphi will help us change, as long as it is, it is possible:

VAR LEN: Integer;

Len: = node.nodevalue;

Of course, we have to pay attention to, it is best to first view the NodeType properties of the same node when reading this method, because there are several types of nodes to avoid exceptions, the controls are as follows:

Nodetype

NodeValue

NTATTRIBUTE attribute node

Attribute value

NTELEMENT element node

If this node does not have a child node, it returns its text, otherwise there is an abnormality.

NTText text node

Text content

The above is all the values ​​of reading and writing nodes, sometimes reading and writing node names, just access Nodename properties, usually use the same way, but also combine NodeValue to use:

Nodetype

Nodename

NTATTRIBUTE attribute node

Name of the attribute

NTELEMENT element node

Node name

NTText text node

'#Text'

After reading, of course, we are doing through the properties of the interface. The interface also has a method to complete, and it will not be said, and the above properties are readable, so modification is basically clear.

(3) Delete

Delete mainly gives the delete specified node, mainly to see several methods, Clear and Delete methods of the IXMLNodeList interface, where the previous one is clear, the next is to delete the specified child node, this method is overloaded, can be used It can also be sequence numbers, namely count attributes, very convenient!

Second, the control parsing method:

Select an XML template file for the FileName property of the control, then select XML Data Binding Wizard by hitting the control ..., an XDB file is created in this wizard. In the pop-up wizard, in the first page, you can see that Delphi has helped you corresponding to each node in the XML file, here you can expand each node, edit the properties data type of each node. Click Next to the next configuration page, here you can see the Delphi generated frame code for each node interface. Click FINSH to complete the wizard so that Delphi will generate an XDB file and an XML interface unit corresponding to the XML document structure. When you have access to this class XML file later, you only need to read the node with reference to the method and attributes generated in the XML interface unit:

VAR XML: IXMLXMLPACKAGETYPE; / / Root interface is generated by wizard

Begin

XML: = loadingXMLPACAGE ('F: / Work / Fah file /task/xml/LAB_XML/TEST.XML'); // This method is also generated in the interface unit.

Xml.data.row [1] .name; //, for example, I want to get the Name property in the second ROW under DATA. Very convenient, you can completely you can use this interface unit, and others can don't. In addition to the loadXmlpacage method in the interface unit, there is a NewXMLPACAGE method and getXMLPACAGE method. These three methods can be used to obtain the XML instance object that is just starting, and all new interfaces in this unit are also inherited the IXMLNode interface, so you don't have to worry about the method. not enough. Of course, only XML instances of similar structures can be accessed, and other formats should recreate a corresponding XML interface unit.

time:

2004-9-3

Author: Fei Asia

转载请注明原文地址:https://www.9cbs.com/read-120205.html

New Post(0)