| This class represents the format base class for XML serialization and
deserialization.
Application classes typically define a default XML format for their
instances using static
XMLFormat class members.
Formats are inherited by sub-classes. For example:[code]
public abstract class Graphic {
private boolean _isVisible;
private Paint _paint; // null if none.
private Stroke _stroke; // null if none.
private Transform _transform; // null if none.
// XML format with positional associations (members identified by their position),
// see XML package description for examples of name associations.
private static final XMLFormat XML = new XMLFormat(Graphic.class) {
public void write(Graphic g, OutputElement xml) {
xml.setAttribute("isVisible", g._isVisible);
xml.add(g._paint); // First.
xml.add(g._stroke); // Second.
xml.add(g._transform); // Third.
}
public void read(InputElement xml, Graphic g) {
g._isVisible = xml.getAttribute("isVisible", true);
g._paint = xml.getNext();
g._stroke = xml.getNext();
g._transform = xml.getNext();
return g;
}
};
}[/code]
Due to the sequential nature of XML serialization/deserialization,
formatting/parsing of XML attributes should always be performed before
formatting/parsing of the XML content.
The mapping between classes and XML formats is defined by
XMLBinding instances.
Here is an example of serialization/deserialization:[code]
// Creates a list holding diverse objects.
List list = new ArrayList();
list.add("John Doe");
list.add(null);
Map map = new FastMap();
map.put("ONE", new Integer(1));
map.put("TWO", new Integer(2));
list.add(map);
// Creates some aliases to use instead of class names.
XMLBinding binding = new XMLBinding();
binding.setAlias(FastMap.class, "Map");
binding.setAlias(String.class, "String");
binding.setAlias(Integer.class, "Integer");
// Formats the list to XML .
OutputStream out = new FileOutputStream("C:/list.xml");
XMLObjectWriter writer = new XMLObjectWriter().setOutput(out).setBinding(binding);
writer.write(list, "MyList", ArrayList.class);
writer.close();[/code]
Here is the output list.xml document produced:[code]
[/code]
The list can be read back with the following code:[code]
// Reads back to a FastTable instance.
InputStream in = new FileInputStream("C:/list.xml");
XMLObjectReader reader = new XMLObjectReader().setInput(in).setBinding(binding);
FastTable table = reader.read("MyList", FastTable.class);
reader.close();[/code]
Note: Any type for which a text format is
TextFormat.getInstance(Class) known can be represented as
a XML attribute.
author: Jean-Marie Dautelle version: 5.1, July 4, 2007 |