User's Guide

Generating XML from Objects

The output serialization API generates XML from Smalltalk objects. The basic method of XML generation is the abtXmlPrintString method. The amount of information provided to the output serialization API affects the default behaviour of the abtXmlPrintString method as follows:

Attribute specification
If the object has an attribute specification, the XML output serialization uses the feature names instead of the instance variable names. Only the features returned by the abtXmlPrintFeatures method are used. Users can override abtXmlPrintFeatures. The implementation of abtXmlPrintFeatures in Object returns all attributes except self.

Mapping specification
If the object has a mapping specification, the XML output serialization uses the element tag name instead of the class name. All mapped attributes are included instead of all features. All attribute mappings are treated like attributes (even if they are text subelements). See Mapping Attributes.

DTD
If the object has a DTD, the XML output serialization can generate the attribute mapping as an attribute or a text subelement. When the object does not have a DTD, all attribute mappings generate attributes.
Note:The XML output serialization must have a mapping specification in order to use the DTD. If there is no mapping specification, the DTD will not be used.

If no information is provided to the output serialization API, the instance variables are mapped to subelements. The class name and each instance variable are generated as an XML start and end tag with the value of the variable printed between the two tags. For example, execution of the following code, (Point x:5 y:10) abtXmlPrintString results in the following XML:

<Point>
<x>5</x>
<y>10</y>
</Point>

The more information you provide, the closer the generated XML is to validated XML.

Note:The VisualAge Smalltalk XML output serialization provides well formed but not validated XML.

Output Serialization Methods

The XML output serialization determines if the mapping specification and DTD are available by sending methods to the class of the object that is being printed. To enable your object to use a mapping specification and DTD, you must implement the abtXmlMappingSpecification and abtXmlDtd methods on your class. Most applications would then look up the mapping specification or DTD in the XML object cache using the following Smalltalk code:

^AbtXmlObjectCache current mappingSpecNamed: 'MySpecName'.

The following methods, which customers can override, are provided in the Object class:

abtXmlPrintFeatures
Returns the features to include when printing an object as XML. Users can override this method if the implementation does not answer the required features. For example, users might override abtXmlPrintFeatures to remove certain features if those features should not be included in the XML.

abtXmlPrintOn: aStream
The abtXmlPrintOn: method works in the same way as the Smalltalk printOn: method. The abtXmlPrintOn: method takes a WriteStream, aStream, for an argument and outputs its XML representation to the passed stream.

abtXmlPrintOn: mappingSpecification:dtd:
This method is only used if a mapping specification and DTD are specified.

abtXmlPrintOn: mappingSpecification:
This method is only used if a mapping specification is specified. If the mapping specification is nil, abtXmlPrintOn: is called.

abtXmlPrintString
The abtXmlPrintString method relies on abtXmlPrintOn (similar to how the printString method relies on the printOn method).

Changing the DOM and regenerating XML

The DOM classes contain implementations of the abtXmlPrintOn: method. This allows you to print out the XML that is parsed. For example, you can parse XML, change the DOM, and print the modified DOM as XML. If the original XML was validated XML, it is still validated XML.

Generating XML from the DOM

You can generate XML from a DOM without the need to use the mapping support. The mapping support supplied with VisualAge is only necessary when mapping parsed DOM objects into custom business objects.

For example, you can parse an XML stream containing the representation of a 'Customer' and derive an instance of a Smalltalk 'Customer' object. If the DOM is manipulated directly, there is no need for mapping. You can send the message abtXmlPrintString to a DOM document instance to derive the XML string that represents that DOM.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]