When building applications that use XML for data interchange, application developers will likely find it preferable to work with objects that relate to the domain of their application rather than being forced to deal with DOM elements that hold data relating to the application domain. In many cases, there is not complete parity between XML data and the domain objects that the XML represents. VisualAge Smalltalk XML support provides a mapping layer to resolve such disparities.
You can create mapping specification files in XML by applying the structure rules defined in the mapping specification DTD (abtxmap.dtd).
VisualAge Smalltalk XML support uses the same mapping specification rules for input deserialization (converting XML to objects) and output serialization (converting objects to XML). Mapping specification files must be constructed based upon the structure rules defined in the DTD (abtxmap.dtd) shown below.
VisualAge Smalltalk XML Support uses the same document type definition (DTD) for input deserialization (converting XML to objects) and output serialization (converting objects to XML). The following DTD, abtxmap.dtd, is provided:
<!ELEMENT XmlMappingSpec ((ClassElementMapping* | ClassTypeMapping*)* | (ClassTypeMapping* | ClassElementMapping*)*)> <!ATTLIST XmlMappingSpec Name NMTOKEN #IMPLIED NameSpaceURI CDATA #IMPLIED> <!ENTITY % ClassMappingAttributes 'Id ID #IMPLIED ParentId IDREF #IMPLIED NameSpaceURI CDATA #IMPLIED ClassName NMTOKEN #REQUIRED' > <!ELEMENT ClassElementMapping (AttributeMapping)*> <!ATTLIST ClassElementMapping ElementTagName NMTOKEN #REQUIRED %ClassMappingAttributes; > <!ELEMENT ClassTypeMapping (AttributeMapping)*> <!ATTLIST ClassTypeMapping TypeName NMTOKEN #REQUIRED %ClassMappingAttributes; > <!ELEMENT AttributeMapping (Attribute | (SubElement, Attribute?)) > <!ATTLIST AttributeMapping ClassAttribute NMTOKEN #IMPLIED GetSelector NMTOKEN #IMPLIED SetSelector NMTOKEN #IMPLIED AttributeClassCreationMethod NMTOKEN #IMPLIED StringConversionMethod NMTOKEN #IMPLIED ObjectToStringConversionMethod NMTOKEN #IMPLIED NameSpaceURI CDATA #IMPLIED Required (true | false) "false" > <!ELEMENT Attribute (#PCDATA) > <!ELEMENT SubElement (#PCDATA) > <!ATTLIST SubElement Key NMTOKEN #IMPLIED>
Mapping specifications are used to map XML elements to Smalltalk objects. The mapping specification is defined as an XML document which conforms to the mapping DTD.
XML consists of elements, which contain attributes and subelements. Smalltalk consists of instances of classes, which have instance variables wrapped by get and set selectors. The mapping specification enables an element attribute to be mapped to an instance variable or an instance variable to be mapped to an element attribute. The same mapping specification objects can be used for converting parsed DOM objects into business objects and for serializing business objects into XML. For information on the mapping specification classes, see Mapping Specification Classes.
The XML parser can be used to parse the mapping XML into a DOM. Code is provided to turn this DOM into instances of our mapping classes.
You can manually create the instances of Smalltalk mapping classes. Manually creating Smalltalk mapping classes allows you to map without parsing a mapping XML. However, if your mapping changes, you would then need to change the Smalltalk code instead of just changing the XML mapping specification file.
The following Smalltalk code uses the mapping specification classes (AbtXmlMappingSpec, AbtClassElementMapping, and AbtAttributeMapping) to create a mapping specification for a customer order. The XML element Order maps to the JrcOrder class. The orderNumber attribute maps to the class attribute order.
| spec ceMap | (spec := AbtXmlMappingSpec new) name: 'CustomerOrder'; addMapping: ((ceMap := AbtClassElementMapping new) elementTagName: 'Order'; className: 'JrcOrder'; addAttributeMapping: (AbtAttributeMapping new classAttribute: 'orderNumber'; attribute: 'number'; classElementMapping: ceMap; yourself); mappingSpec: spec; yourself).