The magic of Merlin: Long lasting

Source: Internet
Author: User
Tags class definition serialization

A new feature of Merlin has been in various forms in Sun's Swing Connection for some time now, in fact it was first discussed at the JavaOne show in 1999. This feature can save JavaBean component state for a long time in an XML document. Serialization work is suitable for short term marshalling needs (for CORBA and RMI) or for storing state information in an executing servlet. However, serialization can cause many problems, including the class library version or the Java runtime environment. The new Xmlencoder/xmldecoder class allows the JavaBean component state to be dumped into a text file for easy modification outside of the Java program, or more likely to generate such a file. Let's take a look at how to use these two classes and how to parse the generated files.

Begin

At the beginning, we need to define a class that we want to initialize, save, and recreate. Let's define a class with the following 4 attributes:

An integer array of test scores, which can be used as an indexed property

Read-only float property, representing average score

String property, representing the student's name

Java.awt.Point attribute, representing the student's seat in the class

This variable set of attribute types will demonstrate how the encoder handles different data types. Listing 1 shows the sample class definition. (It is also in the NET.ZUKOWSKI.IBM package.) See Resources for a download of the code used in this article. There's even a useful toString () method that we can use to visually see that the retrieved value is set correctly.

Save state

Now that we have a class for saving, we can create an instance and save it using Xmlencoder. This class can be found in the Java.beans package, which works in the same way as ObjectOutputStream, but not as part of the OutputStream class hierarchy. You can pass in OutputStream the output stream object you want to save to, and call its WriteObject () method to write the object to the stream. It's simple.

Listing 2. Create an instance and save it as XML // Create
  Sample sample = new Sample();
  sample.setScores(new int[] {100, 90, 75});
  sample.setName("Gore");
  sample.setSeat(new Point(5, 3));
  // Save
  XMLEncoder encoder = new XMLEncoder(
   new BufferedOutputStream(
    new FileOutputStream("Sample.xml")));
  encoder.writeObject(sample);
  encoder.close();

Check format

When you examine the XML file shown in Listing 3, you notice how the read format is encoded with the output, in this case, along with the Xmldecoder in v1.4 Beta. This approach allows future distributions to change format so that if you are using an older XML file, the new decoder will know which encoding type to use when generating the XML file. Essentially, this file is a regular XML file, subject to a specific DTD (not referenced in this article). However, the decoder can recognize the file.

Listing 3. Encoded XML Sample instance <?xml version= "1.0" encoding= "UTF-8"
<java version= "1.4.0-beta"     Java.beans.XMLDecoder ""
<object class= "net.zukowski.ibm.Sample"
<void property= "name"
<string>gore</string>
</void>
<void property= "scores"
<array class= "I NT "length=" 3
<void index= "0"
<int>100</int>
</void>
&L       T;void index= "1"
<int>90</int>
</void>
<void index= "2"
<int>75</int>
</void>
</array>
</void>
<void property= "sea T "
<object class=" Java.awt.Point "
<int>5</int>
<int>3</int> </object>
</void>
</object>
</java>

This particular XML file does not show how to embed method calls that are used to reset bean properties, such as adding listeners and adding components to a container.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.