Menu Content/Inhalt
Start arrow HS Object Kernel arrow HS OK - Articles arrow Basics of the construction and functionality of HSOK
Basics of the construction and functionality of HSOK: XML support Print
20 June 2007
Article Index
THSField class
THSBaseObject class
THSObject class
THSBaseList and descendants
"Weak" and "strong" references
Unicode support
XML support
The kernel object

XML support

To write objects in XML format files or streams we use SaveToXMLFile method implemented in THSBaseObject :

  procedure SaveToXMLFile (const FileName : string;
      XSLSchema : string = '';
      SaveType : THSXMLSaveType = xstDefault;
      CodingType : THSXMLCodingType = xctDefault;
      Stream : TStream = nil);

Besides the name of the file we can specify the name of the schema file (XSLSchema). The SaveType parameter determines whether primitive object fields are to be written as attributes (xstStandard) or as nodes (xstRow). Value xstDefault means, that the write method is determined by a global variable DefXMLSaveType. The CodingType parameter determines the encoding of national characters (xctDefault, xctWin1250, xctUTF8, xctISO88592). Value xctDefault means that the encoding is specified by a global variable DefXMLCodingType. If a destination data stream is given the FileName parameter is ignored.

For reading data in XML format we use LoadFromXMLFile method :

  procedure LoadFromXMLFile (const FileName : string;
      SaveType : THSXMLSaveType = xstDefault;
      Stream : TStream = nil);

For the coding comfort the THSBaseObject class provide SaveToXMLString and LoadFromXMLString methods allowing reading and writing data in XML format to/from a string variable. Data in XML format can be also read and written with the SaveToFile and LoadFromFile methods with the SaveType parameter set to osfXML. In this case we can additionally use compression and/or encryption mechanisms by adding osfZIP and/or osfENC to the SaveType parameter.

In an XML file names of attributes/nodes are represented usually by object's field names. Sometimes though some XML files use names that are not allowed as names of properties in Delphi, because they contain illegal characters (e.g. dot, colon, dash) or are restricted language words (e.g. class, program, out, file, type, unit). In these cases you have to override FieldXMLAliasByIndex and FieldIndexByXMLAlias defined in the THSBaseObject class. The first one returns an XML alias for a field of a given index and is used during the write process. The other method returns the index of a field of a given XML alias and is used while reading. Both these methods are automatically generated and updated by the Class Editor according to data input by the user. The Class Editor supports also the proper definition of these functions while generating classes from imported XML files.