程序包 weka.core.xml

类 XMLSerialization

java.lang.Object
weka.core.xml.XMLSerialization
所有已实现的接口:
RevisionHandler
直接已知子类:
XMLBasicSerialization

public class XMLSerialization extends Object implements RevisionHandler
With this class objects can be serialized to XML instead into a binary format. It uses introspection (cf. beans) to retrieve the data from the given object, i.e. it can only access beans-conform fields automatically.

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.

版本:
$Revision: 1.16 $
作者:
FracPete (fracpete at waikato dot ac dot nz)
另请参阅:
  • 字段详细资料

    • TAG_OBJECT

      public static final String TAG_OBJECT
      the tag for an object
      另请参阅:
    • ATT_VERSION

      public static final String ATT_VERSION
      the version attribute
      另请参阅:
    • ATT_NAME

      public static final String ATT_NAME
      the tag for the name
      另请参阅:
    • ATT_CLASS

      public static final String ATT_CLASS
      the tag for the class
      另请参阅:
    • ATT_PRIMITIVE

      public static final String ATT_PRIMITIVE
      the tag whether primitive or not (yes/no)
      另请参阅:
    • ATT_ARRAY

      public static final String ATT_ARRAY
      the tag whether array or not (yes/no)
      另请参阅:
    • ATT_NULL

      public static final String ATT_NULL
      the tag whether null or not (yes/no)
      另请参阅:
    • VAL_YES

      public static final String VAL_YES
      the value "yes" for the primitive and array attribute
      另请参阅:
    • VAL_NO

      public static final String VAL_NO
      the value "no" for the primitive and array attribute
      另请参阅:
    • VAL_ROOT

      public static final String VAL_ROOT
      the value of the name for the root node
      另请参阅:
    • ROOT_NODE

      public static final String ROOT_NODE
      the root node of the XML document
      另请参阅:
    • ATT_PRIMITIVE_DEFAULT

      public static final String ATT_PRIMITIVE_DEFAULT
      default value for attribute ATT_PRIMITIVE
      另请参阅:
    • ATT_ARRAY_DEFAULT

      public static final String ATT_ARRAY_DEFAULT
      default value for attribute ATT_ARRAY
      另请参阅:
    • ATT_NULL_DEFAULT

      public static final String ATT_NULL_DEFAULT
      default value for attribute ATT_NULL
      另请参阅:
    • DOCTYPE

      public static final String DOCTYPE
      the DOCTYPE for the serialization
  • 构造器详细资料

    • XMLSerialization

      public XMLSerialization() throws Exception
      initializes the serialization
      抛出:
      Exception - if initialization fails
  • 方法详细资料

    • clear

      public void clear() throws Exception
      generates internally a new XML document and clears also the IgnoreList and the mappings for the Read/Write-Methods
      抛出:
      Exception - if something goes wrong
    • getVersion

      public String getVersion()
      returns the WEKA version with which the serialized object was created
      返回:
      the current version
      另请参阅:
    • writeToXML

      public Element writeToXML(Element parent, Object o, String name) throws Exception
      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 method overrideClassname(String) is invoked, which searches for an exact match of the classname in the override hashtable.
      参数:
      parent - the parent of this object, e.g. the class this object is a member of
      o - the Object to describe in XML
      name - the name of the object
      返回:
      the node that was created
      抛出:
      Exception - if the DOM creation fails
      另请参阅:
      • overrideClassname(Object)
      • overrideClassname(String)
      • m_ClassnameOverride
    • toXML

      public XMLDocument toXML(Object o) throws Exception
      extracts all accesible properties from the given object
      参数:
      o - the object to turn into an XML representation
      返回:
      the generated DOM document
      抛出:
      Exception - if XML generation fails
    • readBooleanFromXML

      public boolean readBooleanFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readByteFromXML

      public byte readByteFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readCharFromXML

      public char readCharFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readDoubleFromXML

      public double readDoubleFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readFloatFromXML

      public float readFloatFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readIntFromXML

      public int readIntFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readLongFromXML

      public long readLongFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readShortFromXML

      public short readShortFromXML(Element node) throws Exception
      builds the primitive from the given DOM node.
      参数:
      node - the associated XML node
      返回:
      the primitive created from the XML description
      抛出:
      Exception - if instantiation fails
    • readFromXML

      public Object readFromXML(Object o, String name, Element child) throws Exception
      adds the specific node to the object via a set method
      参数:
      o - the object to set a property
      name - the name of the object for which to set a property (only for information reasons)
      child - the value of the property to add
      返回:
      the provided object, but augmented by the child
      抛出:
      Exception - if something goes wrong
    • readFromXML

      public Object readFromXML(Element node) throws Exception
      builds the object from the given DOM node. (only public due to reflection)
      参数:
      node - the associated XML node
      返回:
      the instance created from the XML description
      抛出:
      Exception - if instantiation fails
    • fromXML

      public Object fromXML(Document document) throws Exception
      returns the given DOM document as an instance of the specified class
      参数:
      document - the parsed DOM document representing the object
      返回:
      the XML as object
      抛出:
      Exception - if object instantiation fails
    • read

      public Object read(String xml) throws Exception
      parses the given XML string (can be XML or a filename) and returns an Object generated from the representation
      参数:
      xml - the xml to parse (if "<?xml" is not found then it is considered a file)
      返回:
      the generated instance
      抛出:
      Exception - if something goes wrong with the parsing
    • read

      public Object read(File file) throws Exception
      parses the given file and returns a DOM document
      参数:
      file - the XML file to parse
      返回:
      the parsed DOM document
      抛出:
      Exception - if something goes wrong with the parsing
    • read

      public Object read(InputStream stream) throws Exception
      parses the given stream and returns a DOM document
      参数:
      stream - the XML stream to parse
      返回:
      the parsed DOM document
      抛出:
      Exception - if something goes wrong with the parsing
    • read

      public Object read(Reader reader) throws Exception
      parses the given reader and returns a DOM document
      参数:
      reader - the XML reader to parse
      返回:
      the parsed DOM document
      抛出:
      Exception - if something goes wrong with the parsing
    • write

      public void write(String file, Object o) throws Exception
      writes the given object into the file
      参数:
      file - the filename to write to
      o - the object to serialize as XML
      抛出:
      Exception - if something goes wrong with the parsing
    • write

      public void write(File file, Object o) throws Exception
      writes the given object into the file
      参数:
      file - the filename to write to
      o - the object to serialize as XML
      抛出:
      Exception - if something goes wrong with the parsing
    • write

      public void write(OutputStream stream, Object o) throws Exception
      writes the given object into the stream
      参数:
      stream - the filename to write to
      o - the object to serialize as XML
      抛出:
      Exception - if something goes wrong with the parsing
    • write

      public void write(Writer writer, Object o) throws Exception
      writes the given object into the writer
      参数:
      writer - the filename to write to
      o - the object to serialize as XML
      抛出:
      Exception - if something goes wrong with the parsing
    • main

      public static void main(String[] args) throws Exception
      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 a toString() of the generated object.
      抛出:
      Exception
    • getRevision

      public String getRevision()
      Returns the revision string.
      指定者:
      getRevision 在接口中 RevisionHandler
      返回:
      the revision