PATH  Documentation > WebObjects 4.5 > XML Reference

up

WOXMLCoding


Implemented by:
Custom objects that need to be encoded as XML
Package:
com.apple.webobjects.xml

Interface Description


When operating without a mapping model, the WOXMLCoder class is capable of encoding a predefined set of Java classes, any object that is an instance of EOEnterpriseObject, and any object that implements the WOXMLCoding interface. This interface consists of a single method, encodeWithWOXMLCoder, in which you encode your object's instance variables using WOXMLCoder's various encode...ForKey methods.

If you'll be reconstituting objects from XML using WOXMLDecoder, your classes must have a constructor that takes a WOXMLDecoder object as its sole argument. This constructor should consist of a series of decode...ForKey method invocations that restore each of your object's instance variables.

The following simple "Person" class implements both the WOXMLCoding interface and the single-argument constructor needed to later decode objects of this class.

import com.apple.webobjects.xml.*;
import com.apple.yellow.foundation.*;
import java.lang.*;
import java.net.*;
import java.math.*;

public class Person extends Object implements WOXMLCoding {
    String name;
    boolean married;
    int children;

    public Person() {
        name = "John Smith";
        married = true;
        children = 2;
    }

    public void encodeWithWOXMLCoder(WOXMLCoder coder) {
        coder.encodeObjectForKey(name, "Name");
        coder.encodeBooleanForKey(married, "MaritalStatus");
        coder.encodeIntForKey(children, "NumberOfChildren");
   }

    // constructor required for decoding
    public Person(WOXMLDecoder decoder) {
        name = (String)decoder.decodeObjectForKey("Name");
        married = decoder.decodeBooleanForKey("MaritalStatus");
        children = decoder.decodeIntForKey("NumberOfChildren");
    }
}

See the XMLArchiving example (accessible through the WebObjects Info Center under Examples > WebObjects > Java > XMLArchiving) for a more complete example illustrating the use of the WOXMLCoding interface.



Instance Methods



encodeWithWOXMLCoder

public abstract void encodeWithWOXMLCoder(WOXMLCoder aCoder)

Implement this method using WOXMLCoder's various encode...ForKey methods (invoked on aCoder) to encode your object's instance variables.

See Also: WOXMLCoder class




up