2005/5/10

     
 

XMLObject[Reader|Writer]

artefaktur

With the XMLObject[Reader|Writer] class you can write and read ACDK objects to a XML stream.



With the XMLObject[Reader|Writer] class you can write and read ACDK objects to a XML stream.
Therefore the Serialization-feature will be used.

Content of this chapter:

   Sample


 Sample


On given class:

ACDK_DECL_CLASS(TestClass);

class ACDK_XML_PUBLIC TestClass 
: extends ::acdk::lang::Object
{
  // need meta information for serialisation
  ACDK_WITH_METAINFO
public:
  /// just a few dummy data elements
  RString _astring;
  /// ACDK-containes can be serialized
  RHashMap _map;
  int _ival;
public:
  /** needed for de-serialisation */
  static RObject create_instance() { return new TestClass(); }
  TestClass()
  : Object(),
    _astring(new String("Hallo")),
    _map(new HashMap()),
    _ival(42)
  {
    _map->put(new String("Kommer"), new String("Roger Rene"));
    _map->put(new String("Reinhard"), new String("Kai"));
  }
  virtual bool equals(RObject o)
  {
    if (instanceof(o, TestClass) == false)
      return false;
    RTestClass other = (RTestClass)o;
    if (_astring->equals(other->_astring) == false)
      return false;
    if (_map->equals(other->_map) == false)
      return false;
    if (_ival != other->_ival)
      return false;
    return true;
  }
};

You can serialize the Class with:

 // create an instance to serialize
  RTestClass object = new TestClass();
  RString fname = "./TestClass.xml";
  {
    RFileWriter fout = new FileWriter(fname);
    // the XMLObjectWriter 
    RXMLObjectWriter xmlout = new XMLObjectWriter(fout);
    // dump out with field information
    xmlout->withFieldInfo(true); 
    xmlout->writeObject(object);
  }
  // and now read in (deserialize) the object from XML-Stream
  
  RFileReader fin = new FileReader(fname);
  RXMLObjectReader xmlin = new XMLObjectReader(fin);
  RObject readedObject = xmlin->readObject();
  if (readedObject->equals(object) == false)
    System::out->println("Test failed");
  else
    System::out->println("Test OK");

The content of ./TestClass.xml:

 <object name="acdk/xml/TestClass" lref="1">
  <field name="_astring">
    <object name="acdk/lang/String" lref="2">Hallo</object>
  </field>
  <field name="_ival">
   <int>42</int>
  </field>
  <field name="_map">
    <object name="acdk/util/HashMap" lref="3">
     <field name="_buckets">
       <object name="[acdk/util/Bucket" lref="4">
        <field name="_length">
         <int>11</int>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <object name="acdk/util/Bucket" lref="5">
           <field name="_first">
             <object name="acdk/util/BucketNode" lref="6">
              <field name="_key">
                <object name="acdk/lang/String" lref="7">Kommer</object>
              </field>
              <field name="_value">
                <object name="acdk/lang/String" lref="8">Roger Rene</object>
              </field>
              <field name="_next">
                <Nil/>
              </field>
             </object>
           </field>
          </object>
        </field>
        <field name="_data">
          <Nil/>
        </field>
        <field name="_data">
          <object name="acdk/util/Bucket" lref="9">
           <field name="_first">
             <object name="acdk/util/BucketNode" lref="10">
              <field name="_key">
                <object name="acdk/lang/String" lref="11">Reinhard</object>
              </field>
              <field name="_value">
                <object name="acdk/lang/String" lref="12">Kai</object>
              </field>
              <field name="_next">
                <Nil/>
              </field>
             </object>
           </field>
          </object>
        </field>
       </object>
     </field>
     <field name="_capacity">
      <int>11</int>
     </field>
     <field name="_loadFactor">
      <float>0.750000</float>
     </field>
     <field name="_modCount">
      <int>2</int>
     </field>
     <field name="_size">
      <int>2</int>
     </field>
     <field name="_threshold">
      <int>8</int>
     </field>
    </object>
  </field>
 </object>