Mapping specifications are used to map XML elements to Smalltalk objects. You can create mapping specifications in XML using the mapping DTD that is provided.
XML consists of elements, which contain attributes and subelements. Smalltalk consists of instances of classes, which have instance variables. 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.
A DTD has been created for writing mapping specifications in XML. The mapping specification is defined as an XML document which conforms to the mapping DTD. The XML parser parses the mapping XML into a DOM. The mapping specification can then be created from the DOM.
VisualAge Smalltalk XML Support uses the same document type definition
(DTD) for input serialization (converting XML to objects) and output
serialization (converting objects to XML). The following DTD,
abtxmap.dtd, is provided:
<!ELEMENT XmlMappingSpec (ClassElementMapping)*>
<!ATTLIST XmlMappingSpec
Name NMTOKEN #IMPLIED>
<!ELEMENT ClassElementMapping (AttributeMapping)*>
<!ATTLIST ClassElementMapping
ElementTagName NMTOKEN #REQUIRED
NameSpaceURI CDATA #IMPLIED
ClassName NMTOKEN #REQUIRED>
<!ELEMENT AttributeMapping (Attribute | (SubElement, Attribute?)) >
<!ATTLIST AttributeMapping
ClassAttribute NMTOKEN #REQUIRED
AttributeClassCreationMethod NMTOKEN #IMPLIED
StringConversionMethod NMTOKEN #IMPLIED>
NameSpaceURI CDATA #IMPLIED
Required (true | false) "false" >
<!ELEMENT Attribute (#PCDATA) >
<!ELEMENT SubElement (#PCDATA) >
<!ATTLIST SubElement
Key NMTOKEN #IMPLIED>
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.
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).