Delphi 6 contains many updated and stronger XML support features. The XML mentioned in the early versions of Delphi is only illustrated by the MIDAS data format and the XMLBroker component (also mentioned in the ClientDataSet and AdodataSet components). The XML feature provided by Delphi 6 adds XML file programming, XML data binding wizard, XML image, and BizSnap (SOAP / XML Web service). This article discusses the first article of the XML function series in Delphi 6, discusses XML Document Programming in Delphi 6. The XML file has just been introduced to the e-commerce worldwide, especially in the B2B (Business-2-Business) field. The reason is that XML is a very simple and structured ASCII text language, anyone can read it. Not the same as the HTML language as the current web standard format. The difference between the two is that HTML is interpreted by a pre-defined syntax set; the XML file is to follow the general syntax rules, but the true keyword (tag) is determined by the author. Its meaning can be selected from the DTD (Document Type DEFINITION) file or now a more popular use (SCHEMA). Using DTD or SCHEMA's XML file can be said to be a self-explanatory file, which is useful when data integration and error adjustment are adjusted. The structured mode of the XML allows data and information to communicate with each other (including between the intermediate layers of the multilayer application, such as DELPHI). It provides a standard format that is transparent to the communication protocol used. This is why XML plays an important role in Electronic Data Interchange, EDI and B2B in e-commerce applications. The XML files will be given to anyone with any other person and something, as long as the appropriate XML / DTD / SCHEMA combination is used. It is now increasingly important to integrate existing systems, and XML may become a "language" that talks and understands between systems. This article does not discuss XML involving EDI or B2B. However, it leads to three articles and XML file programming and Delphi 6's XML new features. XML file programming Delphi 6 supports DOM parsing, so we can read (and translated!) And edit any XML files, even in the absence of DTD or Schema, as shown herein. I created a small XML file containing my web content:
DBEXPRESS AND DATASNAP
2002/01/10
WebBroker / InternetExpress and webSnap
2002/01/31
WebSnap and adapters
2002/02/21
Bizsnap and WebServices
2002/03/14
WebSnap and bizsnap
2002/04/04
This XML file will be used as an example of this series of articles (this article and later XML data bindings and XML images). The TXMLDocument component is to be used to use the TXMLDocument component (in the Internet tag of the Delphi 6 component bar). You can drag a component to the form or data module from there. The TXMLDocument component is some attribute worth mentioning. Obviously, the Active property can be used to open an XML file, but we will not be able to use it. The Domvendor property defines the parser of the XML file. On my machine, it is set to "msxml". You can insert other types of Domvendor yourself (actually any interface components that can perform IdomImmement). Before using your own DomvenDor component, you must register it first. Domvendor is a global variable that contains a Domvendor registration table. Such a third party's Domvendor can join this table after registration, allowing the user to select the Domvendor property value of TXMLDocument itself. The third attribute of TXMLDocument is filename, pointing to an XML file (this example is clinics.xml). If the XML file is not directly existing, it is obtained by other conversion, or this attribute value can be set [Translator Note: As long as the parser support is used, it can even point to a URL]. This is the basic usage in EDI and B2B. Under this occasion, it is often not necessary [sometimes it is impossible - translator] stores real XML files. The NodeIndentStr property determines the post-right position of the child node, the default is two spaces, up to eight spaces. This attribute is only valid after setting the DonodeAutoInDent ID in the option (TRUE), and the default state of this identity is non-true (false). There are other identifiers in the component option, such as NodeAutOcreate, Attrnull, AutovRefix, NamespaceDecl, and AutoSave. Autosave enables the components to automatically save the XML file when it is closed. I think this is a very good feature, so in my example, I set it true (its default state is not true). In addition to regular options, the XMLDocument component also has parsing options, such as ResolveExternals, ValidateonParse, PreserveWhitespace, and AsyncLoad, which can be seen from the name. The last attribute of the XMLDocument component is XML, pointing to a string of XML (the omitting number next to the attribute can edit the XML). As I said earlier, this is very useful in B2B multi-layer applications. At this time, the intermediate layer tends to receive the XML string from other applications, and these XML data will be processed or "programmed". XML file programming can activate the TXMLDocument component as long as the settings are slightly set (setting FileName to CLINICS.XML and set DOAUTOSAVE). After activation, you can traverse nodes, read, and edit data. Now we can access individual nodes (IXMLNODE) in XMLDocument, recursively access the child nodes of each node.
For example, you can use a button to get the element value of the first node and write the result to Memo: Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); VAR Clinic: ixmlNode; begin clinic: = xmlDocument1.documentelement.childNodes [0]; Memo1.Lines.clear; memo1.lines.add (clinic.childnodes ['title']. Text); memo1.lines.add (clinic.childnodes ['Date']. Text); Memo1.Lines.Add (Clinic. ChildNodes ['Topics']. Text) End; Adding the index pointer can access other nodes. The following example uses "current" as an index, and increases the value of "current" by looping. I use try-except to prevent circulation. procedure TForm1.Button2Click (Sender: TObject); var Clinic: IXMLNode; begin Inc (current); try Clinic: = XMLDocument1.DocumentElement.ChildNodes [current]; Memo1.Lines.Clear; Memo1.Lines.Add (Clinic.ChildNodes [ 'Title']. Text); Memo1.Lines.Add (Clinic.childNodes ['Date']. Text); memo1.lines.add (clinic.childnodes ['Topics']. Text); Except ON E: Exception DO Memo1.Lines.Add (E.MESSAGE) End end; In addition to traversing nodes, you can also modify the value of the child node. The following code adds the title value of the first node with a "Hot" prefix. procedure TForm1.Button1Click (Sender: TObject); var Clinic: IXMLNode; begin current: = 0; Clinic: = XMLDocument1.DocumentElement.ChildNodes [current]; Memo1.Lines.Clear; Clinic.ChildNodes [ 'Title'] Text.: = 'Hot:' clinic.childnodes ['title']. Text; memo1.lines.add (clinic.childnodes ['title']. Text); memo1.lines.add (clinic.childnodes ['Date']. Text); Memo1.Lines.Add (Clinic.childNodes ['Topics']. Text); END; you can also add the node.