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: 13476 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
-
KOML
fromXML(Document)
toXML(Object)
m_Properties
m_CustomMethods
readPostProcess(Object)
writePostProcess(Object)
readFromXML(Element)
writeToXML(Element, Object, String)
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
the tag whether array or not (yes/no)static final String
default value for attribute ATT_ARRAYstatic final String
the tag for the classstatic final String
the tag for the namestatic final String
the tag whether null or not (yes/no)static final String
default value for attribute ATT_NULLstatic final String
the tag whether primitive or not (yes/no)static final String
default value for attribute ATT_PRIMITIVEstatic final String
the version attributestatic final String
the DOCTYPE for the serializationstatic final String
the root node of the XML documentList of fully qualified property names to suppress any warning messages forstatic final String
the tag for an objectstatic final String
the value "no" for the primitive and array attributestatic final String
the value of the name for the root nodestatic final String
the value "yes" for the primitive and array attribute -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
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 void
for 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 representationboolean
readBooleanFromXML
(Element node) builds the primitive from the given DOM node.byte
readByteFromXML
(Element node) builds the primitive from the given DOM node.char
readCharFromXML
(Element node) builds the primitive from the given DOM node.double
readDoubleFromXML
(Element node) builds the primitive from the given DOM node.float
readFloatFromXML
(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.int
readIntFromXML
(Element node) builds the primitive from the given DOM node.long
readLongFromXML
(Element node) builds the primitive from the given DOM node.short
readShortFromXML
(Element node) builds the primitive from the given DOM node.void
setSuppressWarnings
(boolean suppress) Set whether to suppress warning messages or notextracts all accesible properties from the given objectvoid
writes the given object into the filevoid
write
(OutputStream stream, Object o) writes the given object into the streamvoid
writes the given object into the writervoid
writes the given object into the filewriteToXML
(Element parent, Object o, String name) adds the given Object to a DOM structure.
-
Field Details
-
TAG_OBJECT
the tag for an object- See Also:
-
ATT_VERSION
the version attribute- See Also:
-
ATT_NAME
the tag for the name- See Also:
-
ATT_CLASS
the tag for the class- See Also:
-
ATT_PRIMITIVE
the tag whether primitive or not (yes/no)- See Also:
-
ATT_ARRAY
the tag whether array or not (yes/no)- See Also:
-
ATT_NULL
the tag whether null or not (yes/no)- See Also:
-
VAL_YES
the value "yes" for the primitive and array attribute- See Also:
-
VAL_NO
the value "no" for the primitive and array attribute- See Also:
-
VAL_ROOT
the value of the name for the root node- See Also:
-
ROOT_NODE
the root node of the XML document- See Also:
-
ATT_PRIMITIVE_DEFAULT
default value for attribute ATT_PRIMITIVE- See Also:
-
ATT_ARRAY_DEFAULT
default value for attribute ATT_ARRAY- See Also:
-
ATT_NULL_DEFAULT
default value for attribute ATT_NULL- See Also:
-
DOCTYPE
the DOCTYPE for the serialization -
SUPPRESS_PROPERTY_WARNINGS
List of fully qualified property names to suppress any warning messages for
-
-
Constructor Details
-
XMLSerialization
initializes the serialization- Throws:
Exception
- if initialization fails
-
-
Method Details
-
setSuppressWarnings
public void setSuppressWarnings(boolean suppress) Set whether to suppress warning messages or not- Parameters:
suppress
- true to suppress warnings
-
clear
generates internally a new XML document and clears also the IgnoreList and the mappings for the Read/Write-Methods- Throws:
Exception
- if something goes wrong
-
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:
-
overrideClassname(Object)
overrideClassname(String)
m_ClassnameOverride
-
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
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readByteFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readCharFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readDoubleFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readFloatFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readIntFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readLongFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
readShortFromXML
builds the primitive from the given DOM node.- Parameters:
node
- the associated XML node- Returns:
- the primitive created from the XML description
- Throws:
Exception
- if instantiation fails
-
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
builds the object from the given DOM node. (only public due to reflection)- Parameters:
node
- the associated XML node- Returns:
- the instance created from the XML description
- Throws:
Exception
- if instantiation fails
-
fromXML
returns the given DOM document as an instance of the specified class- Parameters:
document
- the parsed DOM document representing the object- Returns:
- the XML as object
- Throws:
Exception
- if object instantiation fails
-
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 "<?xml" is not found then it is considered a file)- Returns:
- the generated instance
- Throws:
Exception
- if something goes wrong with the parsing
-
read
parses the given file and returns a DOM document- Parameters:
file
- the XML file to parse- Returns:
- the parsed DOM document
- Throws:
Exception
- if something goes wrong with the parsing
-
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
parses the given reader and returns a DOM document- Parameters:
reader
- the XML reader to parse- Returns:
- the parsed DOM document
- Throws:
Exception
- if something goes wrong with the parsing
-
write
writes the given object into the file- Parameters:
file
- the filename to write too
- the object to serialize as XML- Throws:
Exception
- if something goes wrong with the parsing
-
write
writes the given object into the file- Parameters:
file
- the filename to write too
- the object to serialize as XML- Throws:
Exception
- if something goes wrong with the parsing
-
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
writes the given object into the writer- Parameters:
writer
- the filename to write too
- the object to serialize as XML- Throws:
Exception
- if something goes wrong with the parsing
-
main
for testing only. if the first argument is a filename with ".xml" as extension it tries to generate an instance from the XML description and does atoString()
of the generated object.- Throws:
Exception
-
getRevision
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-