Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XML Enumerations with Delphi XML Data Binding Wizard

Tags:

xml

xsd

delphi

I have an XML schema that uses enumerations, but when I look at the generated XML object in Delphi, the enumeration restriction has been dropped. Is there any way to get Delphi to generate the enum and build it into the object?

XSD Snippet:

<xs:simpleType name="enumType" final="restriction">
    <xs:restriction base="xs:NMTOKEN">
        <xs:enumeration value="Each"/>
        <xs:enumeration value="Units"/>
        <xs:enumeration value="Area"/>
        <xs:enumeration value="Payroll"/>
        <xs:enumeration value="Sales"/>
        <xs:enumeration value="TotalCost"/>
        <xs:enumeration value="Other"/>
    </xs:restriction>
</xs:simpleType>

What I would expect to see in Delphi is a field that accepts a enum that is then converted to it's corresponing string when the XML is generated, but that field is just an ordinary string.

like image 565
LostNomad311 Avatar asked Jan 22 '10 17:01

LostNomad311


2 Answers

What you can do is create your own enumerated type with the same string constants as names and use the unit TypInfo with the function GetEnumValue and GetEnumString. This allows you to prefix the names with a few lowercase letters like in other Delphi code:

Value :=  TMyEnum( GetEnumValue( typeinfo( TMyEnum ), Prefix + AString )  )
like image 77
Ritsaert Hornstra Avatar answered Sep 22 '22 20:09

Ritsaert Hornstra


It is not possible for the XML data binding wizard to do what you want.

The reason is that enumerations in an XSD are not compatible with delphi identifiers because they:

  1. can contain characters incompatible with a Delphi identifier
  2. are case sensitive

Basically XSD enumerations are just strings with a constrained of values.

See the enumeration specs and an example.

Both are clearly incompatible with Delphi enumeration types.

Edit: 20100125 - Delphi attributes

Here is an interesting question on how far you could go with the new attribute and RTTI support in Delphi 2010.

--jeroen

like image 21
Jeroen Wiert Pluimers Avatar answered Sep 20 '22 20:09

Jeroen Wiert Pluimers