Delphi 6 XML data binding

zhaozj2021-02-16  41

Delphi 6 contains many updated XML support features, adds XML file programming, XML data binding wizards, XML images, and BizSnap (SOAP / XML Web services). I am in the previous article discusses XML file programming in Delphi 6. This article discusses the second article of the XML function series in Delphi 6, discusses the XML data binding in Delphi 6 (XML Data Binding).

XML file programming XMLDocument components allow us to traverse and edit the XML file. But in the previous article, I mentioned that we can only deal with non-type nodes, you must remember the name of the node elements. This means that real-time compilation debugging is not available! Fortunately, if Delphi can only handle such a simple problem, it is not a Delphi. The content related to using XML can do more advanced applications, which is the XML Data Binding Wizard of Delphi 6 (XML Data Binding Wizard).

XML Data Binding You can find XML Data Binding Wizard (XML Data Binding Wizard) in the module repository of Delphi 6. Programmers can use it to generate the corresponding interfaces and classes to access and modify XML file data, such as ClientDataSetXML data, ADO XML data, other XML file data (such as clinic.xml used in the previous article, this article continues to use this simple XML File is active).

Start now, start Delphi 6, select File | New - Other on the main menu, then select XML Data Binding in the warehouse, as shown in Figure 1.

There are three pages in the wizard. The first page defines the XML program (SCHEMA) or XML file (this example is CLINIC.XML), as shown in Figure 2. Enter an XML (Schema) or XML file in the resource input box. "Options" dialog box defines the encoding option and the data type mapping relationship (DATA TYPE MAP). We will also talk about these options.

The second page of the wizard shows the tree structure and the node data type (that is, what is the code generated by the wizard). Figure 3 can see my XML file structure. You can see the nested node (ClinicStype and Clinictype) and single node (String) described in the XML file. At this time, you can open the Options dialog (Figure 4) to modify the encoding (such as modified prefix) and data type mapping.

The wizard's third page shows the generated respective interfaces and classes. These results can be saved to a file (for example, generating clinic.xdb). The result (stored as a CLINIC.XDB file) is shown below. We get a ClinicsType type Clinics element, including a Clinic Series element of a CLINICTYPE.

Files simultaneously define ClinicType types of CLINIC elements that contain a series of string elements (Title, Date, and Topics).

The code generated by the code wizard can be used directly in the application. Unfortunately, Delphi 6 sometimes produces an error message for "illegal operation". Once again, it works again. The following is a generated code:

{********************************************************** ***} {} {Delphi XML Data Binding} {} {generated on: 2001/11/07 00:37:00} {generated from: d: /d6clinic/src/clinic.xml} {settings store: d } {*********************************************************** **************} Unit Clinic; Interface USES XMLDOM, XMLDOC, XMLINTF; TYPE

{Forward decls}

IXmlClinicsType = interface; ixmlclinictype = interface;

{Ixmlclinicstype}

IXMLClinicsType = interface (IXMLNodeCollection) [ '{06723E03-662D-11D5-81CE-00104BF89DAD}'] {Property Accessors} function Get_Clinic (Index: Integer): IXMLClinicType; {Methods & Properties} function Add: IXMLClinicType; function Insert (const Index : Integer: ixmlclinicType; Property Clinic [Index: Integer]: IXMLClinictYpe Read GET_CLINIC; DEFAULT; END;

{Ixmlclinictype}

IXMLClinicType = interface (IXMLNode) [ '{06723E04-662D-11D5-81CE-00104BF89DAD}'] {Property Accessors} function Get_No: WideString; function Get_Title: WideString; function Get_Date: WideString; function Get_Topics: WideString; procedure Set_No (Value: WideString); procedure Set_Title (Value: WideString); procedure Set_Date (Value: WideString); procedure Set_Topics (Value: WideString); {Methods & Properties} property No: WideString read Get_No write Set_No; property Title: WideString read Get_Title write Set_Title; Property Date: WideString Read_Date Write Set_date; Property Topics: WideString Read Get_topics Write Set_topics; End; {Forward Decls}

TXMLCLINICSTYPE = Class; TXMLClinictype = Class;

{TXMLCLINICSTYPE}

TXMLClinicsType = class (TXMLNodeCollection, IXMLClinicsType) protected {IXMLClinicsType} function Get_Clinic (Index: Integer): IXMLClinicType; function Add: IXMLClinicType; function Insert (const Index: Integer): IXMLClinicType; public procedure AfterConstruction; override; end;

{TXMLCLINICTYPE}

TXMLClinicType = class (TXMLNode, IXMLClinicType) protected {IXMLClinicType} function Get_No: WideString; function Get_Title: WideString; function Get_Date: WideString; function Get_Topics: WideString; procedure Set_No (Value: WideString); procedure Set_Title (Value: WideString); procedure Set_Date (Value: WideString); Procedure Set_topics (Value: WideString); END;

{Global functions}

Function getClinics: IXmlClinicsType; Function LoadClinics (const filename: wideString): IXMLCLINICSTYPE; Function NewClinics: IXMLClinicsType;

Here are two interface types: IXmlClinicsType and IXMLClinictype; use two classes (TXMLCLINICSTYPE and TXMLCLINICTYPE) to perform this two interfaces. There are also three full-game functions: getClinics, loadClinics (loaded from external XML files) and NewClinics (new files in the memory). Usage is easy to use the generated clinic.PAS unit. Like the procedure of the previous article, use the XMLDocument component (within the inrnenet tag). However, we no longer use the non-type node, we can call the getClinics function to get the IXMLCLinicsType type. The following is the specific operation process:

Establish a new application in the XML Data Binding Guide in Delphi 6 (Naming Save-Translator) Add an XMLDocument component on the main form under the XML Data Binding Wizard Guide. The filename property is clinic.xml. The following code is added to the oncreate event of the main form: Procedure TFORM1.FormCreate (Sender: TOBJECT); VAR Clinics: ixmlclinicstype; begin clinics: = getClinics (xmlDocument1);

It is useful to put Clinics variables in the main form, so you can use the Clinics interface during the main form of operation. It is more convenient than the previous use of ordinary XMLDocument components using the IXmlClinicsType variable. You can now get each CLINIC element through the GET_CLINIC method, and you can insert a new Clinic element in a specific location. Use Clinics.Clinic to get node elements, you can get or set up an element value with getter and setter methods. You can now directly access the properties such as NO, Title, Date, Topics: Procedure TFORM1.BUTTONGETCLICK (Sender: TOBJECT); VAR Clinic: ixmlclinictype; begin clinic: = clinics.clinic [0]; editno.text: = clinic.no; Edittitle.text: = clinic.title; editdate.text: = clinic.date; edittopics.text: = clinic.topics end;

You can see in CLINIC.PAS, getter and setter are methods instead of attribute (in fact, I always think that the use of properties is more clear). But Delphi 6 lets you see that the property description rather than the method itself (another popular advantage of Delphi 6). The above code can be more convenient than the method used by the previous article.

The following example adds a node at the end of the XML tree: Procedure TFORM1.BUTTONADDCLICK (Sender: Tobject); Begin with clinics.add do begin no: = '2001-2-8; // 8th clinic of the 2nd series of 2001 Title : = 'Special Kylix 2 Clinic'; Date: = '2001/12/21'; Topics: = 'KYLIX 2 New Features' end End;

If you do not set the autosave of the XMLDocument component to true, you can save the correction result: Procedure TFORM1.FORMDESTROY (Sender: TOBJECT); begin xmldocument1.savetofile; end; this is the XML data binding wizard, a very convenient method . It can get better and better.

Next article: We have seen the advantages of XML data binding. However, it is not "satisfying", and it is better to traverse each node, access the node value (not only a string type), although the options are determined, but still use the XML image of Delphi 6, it is more Strong. We will be discussed in the next article.

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

New Post(0)