PATH  Documentation > WebObjects 4.5 > XML Reference

up

WOXMLCoder


Inherits from:
Object
Package:
com.apple.webobjects.xml


Class Description


Use this class to encode objects as XML. Encoding can take place either with or without a mapping model. The mapping model provides greater control over the encoding 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 an archiving mechanism, the mapping model is usually not necessary. For more information on the mapping model, see the "The Format of the Mapping Model" in the framework introduction.

When encoding without a mapping model, WOXMLCoder is able to encode any object as long as the object and all of the objects it encapsulates either implement the WOXMLCoding interface or are an instance of String, Number (or a subclass, providing that the subclass doesn't add any new instance variables), NSArray, NSDictionary, NSDate, NSData, or EOEnterpriseObject (or a subclass, providing that all instance variables are either attributes or relationships). During the encoding of an enterprise object, WOXMLCoder uses attribute information stored in the EOModel when assigning an XML type tag to an object. For objects that don't inherit from EOEnterpriseObject, the tag supplied by WOXMLCoder's encodeObjectForKey method is used.

To encode an object, simply invoke the encodeRootObjectForKey method. To perform the reverse operation, generating an object from XML data, see the WOXMLDecoder class.




Method Types


Creating a WOXMLCoder
coder
coderWithMapping
Encoding an object graph
encodeRootObjectForKey
Implementing the WOXMLCoding interface
encodeBooleanForKey
encodeDoubleForKey
encodeFloatForKey
encodeIntForKey
encodeObjectForKey


Static Methods



coder

public static WOXMLCoder coder()

Creates and returns a new WOXMLCoder object.



coderWithMapping

public static WOXMLCoder coderWithMapping(String mappingURL)

Creates and returns a new WOXMLCoder object initialized with the mapping model specified by mappingURL. See "The Format of the Mapping Model" for a complete description of the mapping model.


Windows NT uses backslashes where other systems use forward slashes. When prepending the "file:" URL prefix to a path such as is returned by WOResourceManager's pathForResourceNamed method, on Windows NT the prefix must be "file:\\" while on all other platforms the prefix must be "file://". See the RelatedLinks example for one way to select the proper prefix based upon the underlying system.




Instance Methods



encodeBooleanForKey

public void encodeBooleanForKey(boolean flag, String key)

Invoke from within in your implementation of WOXMLCoding's encodeWithWOXMLCoder method to append an element with XML tag key to the WOXMLCoder object's internal string buffer. The element's XML content is the string representation of flag-either True or False-and the element has an attribute named type with a value of boolean. For example, the following call to encodeBooleanForKey:

encodeBooleanForKey(true, "myTag");

causes the following text to be appended to the WOXMLCoder's internal string buffer:

<myTag type="boolean">True</myTag>



encodeDoubleForKey

public void encodeDoubleForKey(double aDouble, String key)

Invoke from within in your implementation of WOXMLCoding's encodeWithWOXMLCoder method to append an element of type key to the WOXMLCoder object's internal string buffer. The element's content is the string value of aDouble and the element has an attribute named type with a value of double. For example, the following call to encodeDoubleForKey:

encodeDoubleForKey(1.23, "myTag");

causes the following text to be appended to the WOXMLCoder's internal string buffer:

<myTag type="double">1.23</myTag>



encodeFloatForKey

public void encodeFloatForKey(float aFloat, String key)

Invoke from within in your implementation of WOXMLCoding's encodeWithWOXMLCoder method to append an element of type key to the WOXMLCoder object's internal string buffer. The element's content is the string value of aFloat and the element has an attribute named type with a value of float. For example, the following call to encodeFloatForKey:

encodeFloatForKey(1.23, "myTag");

causes the following text to be appended to the WOXMLCoder's internal string buffer:

<myTag type="float">1.23</myTag>



encodeIntForKey

public void encodeIntForKey(int anInt, String key)

Invoke from within in your implementation of WOXMLCoding's encodeWithWOXMLCoder method to append an element of type key to the WOXMLCoder object's internal string buffer. The element's content is the string value of anInt and the element has an attribute named type with a value of int. For example, the following call to encodeIntForKey:

encodeIntForKey(123, "myTag");

causes the following text to be appended to the WOXMLCoder's internal string buffer:

<myTag type="int">123</myTag>



encodeObjectForKey

public void encodeObjectForKey(Object anObject, String key)

Invoke from within in your implementation of WOXMLCoding's encodeWithWOXMLCoder method to append an element of type key to the WOXMLCoder object's internal string buffer. The element's content depends on anObject's class. anObject must meet the same criteria outlined in encodeRootObjectForKey.

encodeRootObjectForKey relies upon this method to perform the actual encoding of objects.



encodeRootObjectForKey

public synchronized String encodeRootObjectForKey(Object anObject, String key)

Encodes anObject as XML and returns the resulting XML string. The encoded root object is tagged using key, and has a type attribute that indicates anObject's class. anObject must be one of the following:

If anObject is not one of the above, encodeRootObjectForKey throws an exception.




up