![]() |
XMLWorks | |
---|---|---|
XMLWorks Online | ||
August 8, 2002 XMLWorks v2.5 is out! InformationXMLWorks is a collection of OpenSource utilities designed to help Delphi developers create XML documents. XMLWorks is in no way designed to be an all encompassing package for XML, but it does offer many features that should help Delphi developers working with XML. |
Current Revision : 2002-08-07
Authors : Marc Bir,
Thomas Weinert (subjective),
Sancho Fock (THS Software)
The best way to learn how XMLWorks can work for you is to view the various samples that come with XMLWorks. Currently 6 sample applications explain the feature set and possible uses of XMLWorks. In addition the XMLandDisplay demo covers XML and rendering in a web browser.
The basic idea behind XMLWorks is to use Delphi Collections to generate DTD and entity information. All you have to do is create a descendant class of TXMLCollection to define your new list type, and a descendant class of TXMLCollectionItem to define the fields of each record.
In general you don't need to do more than declare a descendant class of TXMLCollection named something like TAuthorCollection. When generating the DTD it will automatically use the name 'Author' (whatever text is between the 'T' and 'Collection') + '-list'.
For the TXMLCollectionItem descendant class, you must publish all the properties you want saved in the entity information. Just like the collection class, XMLWorks will automatically use the name between 'T' and 'Collection'.
For example with the following type declarations:
----------------------------------
TAuthorCollection = class(TXMLCollection) end; TAuthorCollectionItem = class(TXMLCollectionItem) private fphone: String; fau_id: String; faddress: String; fcity: String; fpostalcode: String; fau_lname: String; fstate: String; fau_fname: String; ftestint : Integer; ftestchar: char; ftestfloat: real; ftestint64: Int64; ftestenum: TAuthorType; ftestvariant: Variant; published property au_id : String read fau_id write fau_id; property au_lname : String read fau_lname write fau_lname; property au_fname : String read fau_fname write fau_fname; property phone : String read fphone write fphone; property address : String read faddress write faddress; property city : String read fcity write fcity; property state : String read fstate write fstate; property postalcode : String read fpostalcode write fpostalcode; property TestInt : Integer read ftestint write ftestint; property TestFloat : real read ftestfloat write ftestfloat; property TestVariant : Variant read ftestvariant write ftestvariant; property TestInt64 : Int64 read ftestint64 write ftestint64; property TestChar : char read ftestchar write ftestchar; property TestEnum : TAuthorType read ftestenum write ftestEnum; end;----------------------------------
XMLWorks will generate the following DTD
----------------------------------
<!-- Author-list --> <?XML version = "1.0" ?> <!DOCTYPE Author-list [ <!ELEMENT Author-list (Author)* > <!ELEMENT Author (address,au_fname,au_id,au_lname,city,phone,postalcode,state,TestChar,TestEnum,TestFloat,TestInt,TestInt64,TestVariant) > <!ELEMENT address (#PCDATA)> <!ELEMENT au_fname (#PCDATA)> <!ELEMENT au_id (#PCDATA)> <!ELEMENT au_lname (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT phone (#PCDATA)> <!ELEMENT postalcode (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT TestChar (#PCDATA)> <!ELEMENT TestEnum (#PCDATA)> <!ELEMENT TestFloat (#PCDATA)> <!ELEMENT TestInt (#PCDATA)> <!ELEMENT TestInt64 (#PCDATA)> <!ELEMENT TestVariant (#PCDATA)> ]>----------------------------------
A sample entity generated by XMLWorks of the above data
----------------------------------
<Author-list> <Author> <address>9919 Hornpipe</address> <au_fname>Marc</au_fname> <au_id></au_id> <au_lname>Bir</au_lname> <city>Houston</city> <phone>713-827-8931</phone> <postalcode>77080</postalcode> <state>TX</state> <TestChar>Z</TestChar> <TestEnum>atWorthless</TestEnum> <TestFloat>1.1</TestFloat> <TestInt>20</TestInt> <TestInt64>205876666</TestInt64> <TestVariant>TestVariant</TestVariant> </Author> </Author-list>----------------------------------
Example code for using the collections
----------------------------------
with TAuthorCollection.Create(TAuthorCollectionItem) do try with TAuthorCollectionItem(Add)do begin au_fname := 'Marc'; au_lname := 'Bir'; phone := '713-827-8931'; address := '9919 Hornpipe'; city := 'Houston'; state := 'TX'; postalcode := '77080'; TestInt := 20; TestFloat := 1.1; TestVariant := 'TestVariant'; TestInt64 := 205876666; TestChar := 'Z'; TestEnum := atWorthless; end; Memo1.Lines.Add(XML); finally Free; end;----------------------------------
In the above example I create an instance of the Collection then add an item to it, and set the values for the item. This can easily be modified to integrate with TTables(or any data type) in a for loop.
To generate the XML for the data stored in the collection simply read the XML property of the collection. The DTD property is just the Document Type Definition for the collection, and Entities is for
I'm currently working on the reading of the entity information into the collection (you can see the beginnings of this in the demo). The next major feature will be the automatic generation of the class declarations (TAuthorCollection and TAuthorCollectionItem) from a DTD.