The Convertor API allows to convertor object to namespace aware XML fragment and vice versa.
API Overview
The API part is quite straightforward. There is one main class with
couple of static helper methods working on all registered convertors in
the system.
Learn by example
The typical workflow of storing and
loading of an object is illustrated below. It expect that there is
registered convertor for the object which does the actual conversion.
For how to write a convertor see SPI package overview.
1.) test whether there is a convertor for the object
boolean canWrite = Convertors.canWrite(anObject);
2.) if yes then save it to a file
OutputStream os = new FileOutputStream("somefilename.xml");
try {
Convertors.write(os,
anObject);
} finally {
os.close();
}
That's all for writing. The reading is adequately simple:
InputStream is = new FileInputStream("somefilename.xml");
try {
theObject = Convertors.read(is);
} catch (SAXException ex) {
// file is not valid XML document.
// notify user and/or ignore file.
} catch (IOException ex) {
// file cannot be read or there is no convertor for root element
// notify user and/or ignore file.
} finally {
is.close();
}