Briefly analyze XML programming in Delphi

xiaoxiao2021-03-05  28

XML (Extensible Markup Language Scalable Language) In recent years, information magazines are available in recent years, the website is the most eye-catching word. The big and small information products are struggling to have a relationship with it, and they can't catch this quick train. The Borland Series Development Platform with a good reputation is no exception. It is integrated with XML component packages from version 6.0 because it uses the MSXmldom parser, which is more specified, Chinese. Compatibility is better (element name, attribute name supporting Chinese), is very popular among developers. To help beginners quickly master XML programming in Delphi, the author is close to this article for communication.

The author explains the steps of XML programming through an example of reading and writing XML files, and only the reader has nodes, elements, and attributes basic concepts. The XML file structure of the author to read is named INPUT.XML as shown below.

Li Hua

14

6287555

Zhang 3

Age> 16

8273425

The first line of the input.xml file is the version of the XML. The property eNCoding declares what character set is built, default in Unicode encoding (UTF-8 or UTF-16), here is in Chinese GB2312. The second line "" is the root element. Two student elements are defined below, and the students are nestled under the following, which is further illustrative for students. Correspondingly, we define the following student data structures in Delphi, "//" The following text is the description of the variable or statement, the same.

TSTUDENT = Class {Student}

SEX: String; // Student Gender

Name: String; // Student Name

Age: integer; // Student age

Phone: String; // Phone number

END;

To read and write, we need to place two TXMLDocument controls. In the Internet tab of the Delphi VCL panel, the control of the XML word is that, of course, this control can also be created, but you need to include the necessary files, here is simplicity, We are placed directly on the form, named Inxmldoc and Outxmldoc, inxmldoc, used to transfer the input.xml file, OutxMLDoC is used to temporarily output to Output.xml documents.

Put your button on the form and we put the test code directly on the button. First define several variables to save temporary information, as shown below:

Root: ixmlnode; // Pointing XML root node

Parent_node: ixmlnode; // Point to the student node

Child_Node: ixmlnode; // Point to the child's sub-node

Student: TSTUDENT; / / Save a single student information

List: tlist; // 存 学生 list

I: integer; // cycle variable

Let's first read the XML file, the code is as follows:

List: = tlist.create; // Initialize list inxmldoc.loadFromfile ('INPUT.XML'); // Turn into input.xml file

Root: = inxmldoc.documentelement; // Take the root node of the XML file, ie "" Student Relief> "

Parent_node: = root.childNodes.first; // Points Parent_Node points to student nodes

While (PARENT_NODE <> nil) do // Take multiple students in loop, add several student information testing

Begin

IF (PARENT_NODE.NODENAME = 'Student') THEN / / Judgment is the node for students

Begin

Student: = TSTUDENT.CREATE; // Newly built a student structure information

Student.sex: = parent_node.attributes ['Gender']; // Take students' gender properties

Child_node: = parent_node.childnodes.first;

// Make Child_Node points to the first sub-node information of the student

While (child_node <> nil) do // Take each child of students

Begin

IF (child_node.nodeename = 'Name ") THEN / / Judgment is the name node

Student.name: = child_node.text // Take the value of the name node, taken in the Name field

Else if (child_node.nodeename = 'age') THEN / / The four lines of this line are similar to the first two lines

Student.age: = start_node.text)

Else IF (child_node.nodeename = ') THEN

Student.Phone: = child_node.text;

Child_Node: = child_node.nextsibling; // Sequentially remove a student's sub-node information

END;

List.add (student); // Add a student information to the list

END;

Parent_node: = parent_node.nextsibling; // Sequential to remove a student information

END;

Here, all student information has been stored in the list list, and the reader can track the code test.

Below we save the temporary student information in the list to the Output.xml file, the code is as follows:

Outxmldoc.active: = true; // Activate Outxmldoc, automatically initialize empty XML document

Outxmldoc.Encoding: = 'GB2312'; // Setting the character set

Root: = Outxmldoc.addchild ('Student Relief'); // Building a node

For i: = 0 to list.count - 1 do // Take each student information

Begin

Student: = list.Items [i]; // Sequential to take a student information

IF (student <> nil) THEN

Begin

Parent_node: = root.addchild ('student'); // Add a student node after root node

Parent_node.attributes ['Gender']: = student.sex; // Set Gender Properties for Students Node

Child_Node: = parent_node.addchild ('Name'); // Add a name NODE.TEXT: = student.name; // Set the text value of the name

Child_Node: = parent_node.addchild ('age'); // The four lines of this line look similar to the first two lines

Child_Node.Text: = INTOSTR (student.age);

Child_Node: = Parent_Node.Addchild ('Phone');

Child_node.text: = student.Phone;

END;

END;

Outxmldoc.savetofile ('output.xml'); // stores an organization's XML document in an Output.xml file

Outxmldoc.active: = false; // Passivation (close) Outxmldoc

List.free; // Last release of the preservation of temporary student information

Ok, see if INPUT.XML and OUTPUT.XML are the same. Is it very simple? In fact, there is also the same components in C Builder, just convert the Pascal syntax into a C syntax, the above code is completely used.

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

New Post(0)