Delphi XML analytical control in the use of TXMLDocument

  Delphi controls in the use of TXMLDocument 

  Delphi in the XML file is doing a good job of analysis than the direct use of MS MSXML2_TLB interface to facilitate the many, it is described below. 

  Speaking before a given XML example, in certain parts of speakers is relatively easy to understand with examples. 

  1 <? Xml version = "1.0" encoding = "gb2312?"> <! - Documents version information, the same HTML format Notes -> 

  2 <XMLPackage> 

  3 <clinetinfo ip = "202.101.    100.90 "    Handler = "si" unit = "Jiangsu network to software" /> 

  4 <data> 

  5 <row id = "    1 "    Name = "Sun" sex = "M" age = "24" duty = "software engineer" /> 

  6 <row id = "    2 "    Name = "moon" sex = "woman" age = "25" duty = "manager" /> 

  7 <! - Data elements row contains two examples, through distinction between id attribute -> 

  8 </ data> 

  9 <memo length = "    16 "    Color = "$ 0034494B"> Hello! I am yaya! </ Memo> <! - This element contains the contents of a middle Text, but also contains two attributes length, color, of course, can not attribute -> 

  10 <Actions acition="update/insert"/> <! - Describes the elements of a motor control information -> 

  11 </ XMLPackage> 

  TXMLDocument component provides a common practice our two: First, the direct use of this class encapsulates the properties and methods to read and write XML documents, I call it their own analytical method; Second, the controls used XML Data Binding Wizard to the creation of a suitable user interface module, and then follow this interface modules provide interfaces to achieve many of the same XML document to read and write, is very convenient, but there are limitations, I call it control analytical method.    I focuses on the first use. 

  First, their analytical method: 

  Now I will create, read, modify, add, delete and move to five points on three categories. 

  (1) create and add: 

  First to create XML example, can create dynamic: 

  TXMLDocument.Create ( 'F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ country.xml'); 

  This dynamic can be created: 

  Xml: = TXMLDocument.Create (nil); 

  Xml. LoadFromFile ( 'F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ country.xml'); 

  From the Internet can certainly drop a TXMLDocument panel controls, and then for the FileName property assignment or XML, for example, were still called xml objects (this is not the default): 

  Xml.FileName: = F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ country.xml; 

  As for the other attributes XML is used to direct vested XML language, the usage of our not important.    End creating XML instance, we should pay attention to open Active Properties: 

  Xml.active: = true; 

  Below this the document to effectively read and write operations.    If there is no XML document can be used to create XML examples, that is, to the establishment of an XML document, you can choose one above, the operation of XML example, after completion of operation: 

  Xml.SaveToFile ( 'F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ country.xml'); 

  Below is to create nodes, requires a combination of the above described examples of XML documents.    First an XML document there can be only one root, for example, documents examples of the two firms is such XMLPackage been created: 

  Var rootnode: XMLNode; 

  Rootnode: = xml.CreateNode ( 'XMLPackage'); 

  Xml. DocumentElement: = rootnode; 

  Then we create different types of sub-nodes, in front of the first examples of the node to 9 more representative, it is a text of a node, at the same time, with two attribute node, we have to look at how it is it was created, first of all, to create a root node: 

  Xml.Active: = true; 

  Xml.DocumentElement: = xml.CreateNode ( 'XMLPacage'); 

  Then we created memo-node: 

  Var node: IXMLNode; 

  Node: = xml.CreateNode ( 'memo'); 

  Xml.DocumentElement.ChildNodes.Add (node); 

  This son of nodes to create a better approach, of course, can also: 

  Xml.DocumentElement.AddChild ( 'XMLPacage'); 

  To create, recommend the use of the first, CreateNode method is the use of rich, mainly to see its second parameter, is above using the default parameters Now I look at how to create the text of this sub-nodes: 

  Node.ChildNodes.Add (xml.CreateNode ( 'Hello! I am yaya!' NtText)); 

  CreateNode attention to the second method parameters, we continue to look at how to create attribute is: 

  Node.AttributeNodes.Add (xml.CreateNode ( 'length', ntAttribute)); 

  Node.SetAttribute ( 'length', 16); 

  Node.AttributeNodes.Add (xml.CreateNode ( 'color', ntAttribute)); 

  Node.SetAttribute ( 'color', $ 0034494B); 

  Finally do not forget to preserve oh: 

  Xml.SaveToFile ( 'F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ test.xml'); 

  Xml.Active: = false; 

  To this end we create, as add that the insert nodes, to be planted on the specified above will add nodes (const Node: IXMLNode) method changed Insert (Index: Integer; const Node: IXMLNode), and the other used it at a glance not elaborated. 

  (2) read and modify 

  Read relatively simple, mainly reading of the text nodes and attributes value method is relatively simple, the key is to utilize IXMLNodeList interface to provide a number of properties and methods to cycle search to a specific node on OK.    Attribute value of the text and read in the delphi method provided in a lot of ways, I would say a: NodeValue attributes, it is an attribute IXMLNode interface, and its value is OleVariant types means that we are in the XML file, although all is a string of, but delphi will help us to transform data format, for example, attribute node is a node: 

  Node.NodeValue: = 16; / / 16 is the integer type 

  Although the XML document is saved in ASCII format 16, but we have to read the time for delphi will help us come back, and as long as this can be: 

  Var len: integer; 

  Len: = node.NodeValue; 

  Of course, we must pay attention to is that this way of reading the best time to check the same node NodeType property, because there are several types of nodes to avoid abnormal control as follows: 

NodeType

NodeValue

  NtAttribute attribute node 

  The value of the attribute 

  NtElement element nodes 

  If the node has no children nodes, it will return the TEXT, or abnormal 

  NtText text node 

  TEXT content 

  The above are read, and write the value of nodes, nodes were sometimes have to read and write, will be visiting NodeName attributes, probably using the same method, but also with NodeValue to use: 

NodeType

NodeName

  NtAttribute attribute node 

  Attribute names 

  NtElement element nodes 

  Node names 

  NtText text node 

  '# Text' 

  Read on the end, of course, we are above the interface properties do, and also there is a way to interface completed, not say, several of the above properties are readable can be written, so the amendment basically clear. 

  (3) Delete 

  Delete key to delete specified node is given, mainly to see IXMLNodeList Interface several ways, Clear and Delete methods, the first one is all empty, and the latter one is to delete the specified nodes of this method is overloaded, by name or by the order of attributes that Count's very convenient. 

  Second, control analytical method: 

  Controls for the FileName property choose an XML document templates, and then hit by the control options XML Data Binding Wizard…, in the middle of this guide will create a xdb document.    In the pop-up wizard, the first page, we can see that Delphi has been to help you in all XML document node corresponds to the interface, where you can start each node, the node edit the properties of each data type.    Click Next to a configuration page, and here you can see the various nodes Delphi generated in the framework interface code.    Click Finsh complete the wizard, this will generate a Delphi xdb documents and XML document structure and the corresponding XML interface unit.    Later visit to the XML documents, the only reference to the XML interface unit generated in the methods and properties can be easily read node: 

  Var xml: IXMLXMLPackageType; / / generated by the root of the wizard interface 

  Begin 

  Xml: = LoadXMLPacage (''F: \ Work \ charge of the paper \ task \ XML \ lab_xml \ test.xml '); / / This method is generated in the interface unit. 

  Xml.Data.Row [1]. Name; / / For instance, I would like to obtain data under section 2 row in the name attribute. 

  Very convenient, and can fully as long as such an interface unit can be, and what the other can not.    LoadXMLPacage interface unit, in addition to ways and methods are NewXMLPacage GetXMLPacage, these three methods can be used to obtain the beginning of the XML object example, this unit is all new interface inheritance IXMLNode interface, so you not have to worry about the method used.    Of course, can only access the similar structure of XML example, other formats have to re-create a corresponding XML interface unit can. 

  Time:  2004-9-3

  Author: fee - 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “Delphi XML analytical control in the use of TXMLDocument”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.