java.lang.Object java.beans.Encoder java.beans.XMLEncoder
All Implemented Interfaces:
XMLEncoderclass is a complementary alternative to the
ObjectOutputStreamand can used to generate a textual representation of a JavaBean in the same way that the
ObjectOutputStreamcan be used to create binary representation of
Serializableobjects. For example, the following fragment can be used to create a textual representation the supplied JavaBean and all its properties:
XMLEncoder e = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("Test.xml"))); e.writeObject(new JButton("Hello, world")); e.close();Despite the similarity of their APIs, the
XMLEncoderclass is exclusively designed for the purpose of archiving graphs of JavaBeans as textual representations of their public properties. Like Java source files, documents written this way have a natural immunity to changes in the implementations of the classes involved. The
ObjectOutputStreamcontinues to be recommended for interprocess communication and general purpose serialization.
XMLEncoder class provides a default denotation for
JavaBeans in which they are represented as XML documents
complying with version 1.0 of the XML specification and the
UTF-8 character encoding of the Unicode/ISO 10646 character set.
The XML documents produced by the
XMLEncoder class are:
XMLEncoderclass uses a redundancy elimination algorithm internally so that the default values of a Bean's properties are not written to the stream.
Below is an example of an XML archive containing some user interface components from the swing toolkit:
<?xml version="1.0" encoding="UTF-8"?> <java version="1.0" class="java.beans.XMLDecoder"> <object class="javax.swing.JFrame"> <void property="name"> <string>frame1</string> </void> <void property="bounds"> <object class="java.awt.Rectangle"> <int>0</int> <int>0</int> <int>200</int> <int>200</int> </object> </void> <void property="contentPane"> <void method="add"> <object class="javax.swing.JButton"> <void property="label"> <string>Hello</string> </void> </object> </void> </void> <void property="visible"> <boolean>true</boolean> </void> </object> </java>The XML syntax uses the following conventions:
Although all object graphs may be written using just these three tags, the following definitions are included so that common data structures can be expressed more concisely:
Integerclass could be written: <int>123</int>. Note that the
XMLEncoderclass uses Java's reflection package in which the conversion between Java's primitive types and their associated "wrapper classes" is handled internally. The API for the
XMLEncoderclass itself deals only with
For more information you might also want to check out Using XMLEncoder, an article in The Swing Connection.
|Fields inherited from java.beans.Encoder:|
public XMLEncoder(OutputStream out, String charset, boolean declaration, int indentation)
|Method from java.beans.XMLEncoder Summary:|
|clear, close, flush, getOwner, setOwner, writeExpression, writeObject, writeStatement|
|Methods from java.beans.Encoder:|
|clear, get, getAttribute, getExceptionListener, getPersistenceDelegate, getValue, remove, setAttribute, setExceptionListener, setPersistenceDelegate, writeExpression, writeObject, writeStatement|
|Methods from java.lang.Object:|
|clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait|
|Method from java.beans.XMLEncoder Detail:|
public void close()
public void flush()
public Object getOwner()
public void writeExpression(Expression oldExp)
This method should only be invoked within the context of initializing a persistence delegate or setting up an encoder to read from a resource bundle.
For more information about using resource bundles with the XMLEncoder, see http://java.sun.com/products/jfc/tsc/articles/persistence4/#i18n
public void writeObject(Object o)
public void writeStatement(Statement oldStm)
This method should only be invoked within the context of initializing a persistence delegate.