Class XMLSerialization
- All Implemented Interfaces:
RevisionHandler
- Direct Known Subclasses:
XMLBasicSerialization
The generic approach of writing data as XML can be overriden by adding
custom methods for reading/writing in a derived class
(cf. m_Properties, m_CustomMethods).
Custom read and write methods must have the same signature (and also be
public!) as the readFromXML and writeToXML
methods. Methods that apply to the naming rule read + property name
are added automatically to the list of methods by the method
XMLSerializationMethodHandler.addMethods(...).
Other properties that are not conform the bean set/get-methods have to be
processed manually in a derived class (cf. readPostProcess(Object),
writePostProcess(Object)).
For a complete XML serialization/deserialization have a look at the
KOML class.
If a stored class has a constructor that takes a String to initialize (e.g. String or Double) then the content of the tag will used for the constructor, e.g. from
<object name="name" class="String" primitive="no">Smith</object>"Smith" will be used to instantiate a String object as constructor argument.
- Version:
- $Revision: 1.16 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringthe tag whether array or not (yes/no)static final Stringdefault value for attribute ATT_ARRAYstatic final Stringthe tag for the classstatic final Stringthe tag for the namestatic final Stringthe tag whether null or not (yes/no)static final Stringdefault value for attribute ATT_NULLstatic final Stringthe tag whether primitive or not (yes/no)static final Stringdefault value for attribute ATT_PRIMITIVEstatic final Stringthe version attributestatic final Stringthe DOCTYPE for the serializationstatic final Stringthe root node of the XML documentstatic final Stringthe tag for an objectstatic final Stringthe value "no" for the primitive and array attributestatic final Stringthe value of the name for the root nodestatic final Stringthe value "yes" for the primitive and array attribute -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()generates internally a new XML document and clears also the IgnoreList and the mappings for the Read/Write-Methodsreturns the given DOM document as an instance of the specified classReturns the revision string.returns the WEKA version with which the serialized object was createdstatic voidfor testing only.parses the given file and returns a DOM documentread(InputStream stream) parses the given stream and returns a DOM documentparses the given reader and returns a DOM documentparses the given XML string (can be XML or a filename) and returns an Object generated from the representationbooleanreadBooleanFromXML(Element node) builds the primitive from the given DOM node.bytereadByteFromXML(Element node) builds the primitive from the given DOM node.charreadCharFromXML(Element node) builds the primitive from the given DOM node.doublereadDoubleFromXML(Element node) builds the primitive from the given DOM node.floatreadFloatFromXML(Element node) builds the primitive from the given DOM node.readFromXML(Object o, String name, Element child) adds the specific node to the object via a set methodreadFromXML(Element node) builds the object from the given DOM node.intreadIntFromXML(Element node) builds the primitive from the given DOM node.longreadLongFromXML(Element node) builds the primitive from the given DOM node.shortreadShortFromXML(Element node) builds the primitive from the given DOM node.extracts all accesible properties from the given objectvoidwrites the given object into the filevoidwrite(OutputStream stream, Object o) writes the given object into the streamvoidwrites the given object into the writervoidwrites the given object into the filewriteToXML(Element parent, Object o, String name) adds the given Object to a DOM structure.
-
Field Details
-
TAG_OBJECT
-
ATT_VERSION
-
ATT_NAME
-
ATT_CLASS
-
ATT_PRIMITIVE
-
ATT_ARRAY
-
ATT_NULL
-
VAL_YES
-
VAL_NO
-
VAL_ROOT
-
ROOT_NODE
-
ATT_PRIMITIVE_DEFAULT
default value for attribute ATT_PRIMITIVE- See Also:
-
ATT_ARRAY_DEFAULT
-
ATT_NULL_DEFAULT
-
DOCTYPE
the DOCTYPE for the serialization
-
-
Constructor Details
-
XMLSerialization
-
-
Method Details
-
clear
-
getVersion
returns the WEKA version with which the serialized object was created- Returns:
- the current version
- See Also:
-
writeToXML
adds the given Object to a DOM structure. (only public due to reflection).
Note:overrideClassname(Object)is not invoked in case of arrays, since the array class could be a superclass, whereas the elements of the array can be specialized subclasses. In case of an array the methodoverrideClassname(String)is invoked, which searches for an exact match of the classname in the override hashtable.- Parameters:
parent- the parent of this object, e.g. the class this object is a member ofo- the Object to describe in XMLname- the name of the object- Returns:
- the node that was created
- Throws:
Exception- if the DOM creation fails- See Also:
-
toXML
extracts all accesible properties from the given object- Parameters:
o- the object to turn into an XML representation- Returns:
- the generated DOM document
- Throws:
Exception- if XML generation fails
-
readBooleanFromXML
-
readByteFromXML
-
readCharFromXML
-
readDoubleFromXML
-
readFloatFromXML
-
readIntFromXML
-
readLongFromXML
-
readShortFromXML
-
readFromXML
adds the specific node to the object via a set method- Parameters:
o- the object to set a propertyname- the name of the object for which to set a property (only for information reasons)child- the value of the property to add- Returns:
- the provided object, but augmented by the child
- Throws:
Exception- if something goes wrong
-
readFromXML
-
fromXML
-
read
parses the given XML string (can be XML or a filename) and returns an Object generated from the representation- Parameters:
xml- the xml to parse (if "invalid input: '<'?xml" is not found then it is considered a file)- Returns:
- the generated instance
- Throws:
Exception- if something goes wrong with the parsing
-
read
-
read
parses the given stream and returns a DOM document- Parameters:
stream- the XML stream to parse- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
read
-
write
-
write
-
write
writes the given object into the stream- Parameters:
stream- the filename to write too- the object to serialize as XML- Throws:
Exception- if something goes wrong with the parsing
-
write
-
main
-
getRevision
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-