|
WebObjects 5.2 | |||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Interface Summary | |
WOXMLCoding | This interface allows WOXMLCoder, when used without a mapping model, to encode the class that implements this interface as XML data. |
Class Summary | |
WOXMLCoder | This class is used to encode objects as XML data. |
WOXMLDecoder | Use this class to decode an object from XML data. |
Exception Summary | |
WOXMLException | This class serves to wrap a number of exceptions that can arise during the parsing process, reducing the number of exceptions your code has to catch. |
Provides for encoding and decoding of XML data.
The XML framework consists of two main classes-WOXMLCoder and WOXMLDecoder-which can encode and decode objects as XML. These classes can also be used to parse or generate XML data from an external source (such as the World Wide Web). When working with such "foreign" XML, you describe the XML elements and properties and their mapping to objects in an XML-format "mapping model" that you can create with either a text editor or an XML editor.
The mapping model provides greater control over the decoding process and is typically used when you are encoding and decoding XML that is destined for, or originates from, an external source. When the WOXMLCoder and WOXMLDecoder are used as a simple archiving mechanism, the mapping model is usually not necessary. See "The Format of the Mapping Model" for more information on the contents and structure of the mapping model.
When encoding and decoding a custom object using the WOXMLCoder and WOXMLDecoder classes without a mapping model, your custom class needs to:
encodeWithWOXMLCoder
declared in the WOXMLCoding interface (your implementation of this method is where you encode the custom class's instance variables)You don't need to do the above if you are working with a mapping model or the object you are encoding and decoding is:
Exceptions raised by the underlying SAX parser are, for simplicity's sake, wrapped in a WOXMLException object, greatly reducing the number of exceptions your code needs to catch.
The mapping model can be created manually with a text editor or an XML editor. It is simply a text file that consists of one or more <entity> elements, each of which can enclose zero or more <property> elements, all enclosed within a single root <model> element.
The following is a simple mapping model:<model> <entity name="Command" xmlTag="command"> <property name="qty" xmlTag="quantity" attribute="YES"/> <property name="movie" xmlTag="movie"/> <property name="customer" xmlTag="customer"/> </entity> <entity name="MyMovie" xmlTag="movie"> <property name="title" xmlTag="name" attribute="YES"/> <property name="dateReleased" xmlTag="date"> <property name="roles" xmlTag="role"> <property name="category" xmlTag="cat"/> </entity> <entity name="com.webobjects.eocontrol.EOGenericRecord" xmlTag="role"> <property name="roleName" xmlTag="name" attribute="YES"/> </entity> </model>
When creating a mapping model, be aware that mappings must be unique for a given property when decoding (that is, you cannot have two mappings for the same property). The same applies for XML tags when encoding: you cannot have two mappings for the same XML tag.
The <model> element is the root element of the mapping file and it contains the mapping information. It has no attributes. The <model> element must contain at least one <entity> element.
The <entity> element describes the relationship between the generated XML data and the encoded objects. The first <entity> element describes the root element of the generated XML data. Subsequent <entity> elements are usually required to complete the description of the root element. It has two required attributes and a number of optional ones:
name=
property (required)xmlTag=
tag (required)ignoreUnmappedTags="YES" | "NO"
(optional)"NO"
causes a WOXMLException to be thrown if unmappedTagsKey
isn't specified and, while parsing the XML for the entity, an XML tag is encountered for which there is no specified mapping in the mapping model. No exception is thrown if "YES"
is specified or if the entity object being decoded is an NSDictionary."NO".
unmappedTagsKey=
key (optional)'{ quantity = "9"; }'
. The developer has to deal with this as such.contentsKey=
key(optional)... <entity name="com.webobjects.foundation.NSMutableDictionary" xmlTag="text" contentsKey="contents" /> ...
and if the following was encountered while parsing XML:
<text>Hello, <b>World!</b></text>
an NSMutableDictionary object would be created with a single key-value pair: the key "contents" would have as its value an NSMutableArray containing two elements: the string "Hello, " and a dictionary with a single key-value pair: the key "b" would have as its value the string "World!".
When this attribute is specified, it is not necessary to create <property> elements for this <entity> element since they are not used anyway. Instead, ensure that there is a key-value coding pair for the key and that the value is dealt with as an NSMutableArray.Within an <entity> element, you can have zero or more <property> elements. It describes the mapping between the entity object's property and the XML data to be generated for that property. Each entity object's property is a key-value coding pair whose value will end up as XML data. A <property> element has two required attributes and a few optional ones:
name=
key (required)xmlTag=
tag (required)Command
has a property movie
. Since movie
is actually an object of class MyMovie, it is further described using another <entity> element.attribute="YES" | "NO"
(optional)"YES"
causes this property to be encoded as an attribue of the enclosing <entity> element instead of the usual sub-element. For example, if attribute="YES"
was specified for the movie
property, it will be encoded as:
instead of:<command movie="someValue" ... </command>
Default is<command> ... <movie>someValue</movie> ... </command>
"NO".
forceList="YES" | "NO"
(optional)forceList="NO"
). "YES"
causes a single enclosed element to be decoded as an NSMutableArray with a single object of the appropriate type. For example, if we have an XML segment like this:
by default, decoding will result in a key-value coding call with key<property_foo> <type_bar>some_Value</type_bar> </property_foo>
property_foo
and value an object of type type_bar
. But forceList="YES"
causes the value in this instance to become an NSMutableArray containing a single object of type type_bar
.Note that decoding an XML structure like the following:
<property_foo> <type_bar>value1</type_bar> <type_bar>value2</type_bar> </property_foo>
always results in a key-value coding call with key property_foo
and value an NSMutableArray containing the two type_bar
objects.
"NO".
codeBasedOn="TAG"
(optional)codeBasedOn="TAG"
in the property's list of attributes.reportEmptyValues="YES" | "NO"
(optional)<myElement></myElement>
), the WOXMLDecoder creates an empty NSDictionary object by default. Setting reportEmptyValues
to "NO"
prevents this empty object from being created."YES".
outputTags="class" | "property" | "both" | "neither"
(optional)property
, the default, specifies that the property's XML tag should be output. class
specifies that the XML tag associated with the property's enclosing class should be output instead. both
indicates that both the property and the class tags should be output, and neither
indicates that neither XML tag should be placed in the XML.
To illustrate the use of the outputTags attribute, the following mapping model could be used to produce HTML tags from an NSMutableDictionary object:
<model> <entity name="com.webobjects.foundation.NSMutableDictionary" xmlTag="text"> <property name="p" xmlTag="ignore" outputTags="neither"/> </entity> <entity name="Paragraph" xmlTag="p"> <property name="text.contents" xmlTag="text" outputTags="class"/> </entity> <entity name="ItemList" xmlTag="ul"> <property name="contents" xmlTag="li" outputTags="both"/> </entity> <entity name="Item" xmlTag="item"> <property name="contents" xmlTag="ignore" outputTags="class"/> </entity> <entity name="LineBreak" xmlTag="br"/> <entity name="ExternalLink" xmlTag="a"> <property name="url" xmlTag="href" attribute="YES"/> <property name="anchorText" xmlTag="ignore" outputTags="neither"/> </entity> </model>
|
Last updated Fri Feb 21 13:15:00 PST 2003. | |||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |