public abstract class XMLFormat<T> extends Object
A format for XML serialization and deserialization. The default
XML format for any given class can be defined using the
DefaultXMLFormat inheritable annotation.
@DefaultXMLFormat(Graphic.XML.class) public abstract class Graphic implements XMLSerializable { private boolean isVisible; private Paint paint; // null if none. private Stroke stroke; // null if none. private Transform transform; // null if none. public static class XML extends XMLFormat { public void write(Graphic g, OutputElement xml) throws XMLStreamException { xml.setAttribute("isVisible", g.isVisible); xml.add(g.paint, "Paint"); xml.add(g.stroke, "Stroke"); xml.add(g.transform, "Transform"); } public void read(InputElement xml, Graphic g) throws XMLStreamException { g.isVisible = xml.getAttribute("isVisible", true); g.paint = xml.get("Paint"); g.stroke = xml.get("Stroke"); g.transform = xml.get("Transform"); } } }
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 current XML format is retrieved from the XMLContext.
A predefined format exists for Collection and
Map maps.
// Creates a list holding diverse objects. List list = new ArrayList(); list.add("John Doe"); list.add(null); Map map = new FastMap(); map.put("ONE", 1); map.put("TWO", 2); list.add(map); // Use of custom binding. 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();
Here is the output list.xml document produced:
<MyList>
<String value="John Doe"/>
<Null/>
<Map>
<Key class="String" value="ONE"/>
<Value class="Integer" value="1"/>
<Key class="String" value="TWO"/>
<Value class="Integer" value="2"/>
</Map>
</MyList>The list can be read back with the following 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();
| Modifier and Type | Class and Description |
|---|---|
static class |
XMLFormat.InputElement
This class represents an input XML element (unmarshalling).
|
static class |
XMLFormat.OutputElement
This class represents an output XML element (marshalling).
|
| Modifier | Constructor and Description |
|---|---|
protected |
XMLFormat()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
isReferenceable()
Indicates if the object serialized through this format can be referenced
to (default
true). |
T |
newInstance(Class<? extends T> cls,
XMLFormat.InputElement xml)
Allocates a new object of the specified class from the specified
XML input element.
|
abstract void |
read(XMLFormat.InputElement xml,
T obj)
Parses an XML input element into the specified object.
|
abstract void |
write(T obj,
XMLFormat.OutputElement xml)
Formats an object into the specified XML output element.
|
public boolean isReferenceable()
true). This method can be overriden to return
false if serialized objects are manipulated "by value".true if serialized object may hold a reference;
false otherwise.XMLReferenceResolverpublic T newInstance(Class<? extends T> cls, XMLFormat.InputElement xml) throws XMLStreamException
cls - the class of the object to return.xml - the XML input element.XMLStreamExceptionpublic abstract void write(T obj, XMLFormat.OutputElement xml) throws XMLStreamException
obj - the object to format.xml - the XMLElement destination.XMLStreamExceptionpublic abstract void read(XMLFormat.InputElement xml, T obj) throws XMLStreamException
xml - the XML element to parse.obj - the object created through newInstance(java.lang.Class<? extends T>, javolution.xml.XMLFormat.InputElement)
and to setup from the specified XML element.XMLStreamExceptionCopyright © 2017. All rights reserved.