. .

st4u

ST 4U 251: Mapping XML to Objects in VA

June 27, 2012 9:10:24.224

Today's Smalltalk 4 You looks at using a mapping specification (in XML) so that the XML parser in VA will return Smalltalk objects rather than an XML document object. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

Mapping XML.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll look at setting up a mapping of XML to Smalltalk objects via a mapping file. We'll still be using the same orders example we've been looking at; this time, we have the additional mapping file:

Mapping

To run this example, use:


AbtXmlBasicSamples mappedExample


and that code looks like this:


	| mappingDOM mappingSpec result |

	self isDataInFile ifFalse: [self setupData].
	self isMapInFile ifFalse: [self setupMap].
 
	" Create a mapping spec from the order map "
	mappingDOM := AbtXmlDOMParser newNonValidatingParser
		parseURI: 'order.map'.   "The image must contain Smalltalk classes conforming to the map."
	mappingSpec := AbtXmlMappingSpec fromMappingDOM: mappingDOM.
 	
	" Parse the data "
	result := AbtXmlMappingParser newValidatingParser
		mappingSpec: mappingSpec; 
		parseURI: 'order.xml'.
	^result abtXmlMappedObject.

What that does is read the mapping data first, to construct a specification - that will be used when parsing the XML in order to get objects instead of a basic DOM object. The results should look like this:

Orders

If you take a look at the mapping XML file, you'll see that it's pretty easy to understand - setting one up yourself would not be a problem.

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Technorati Tags: , , ,

Enclosures:
[st4u251-iPhone.m4v ( Size: 4067590 )]

posted by James Robertson

 Share Tweet This