java.beans
Class XMLEncoder

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.beans.Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder 
      extended by java.beans.XMLEncoder

public class XMLEncoder
extends Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder

The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects. 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 XMLEncoder class 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 ObjectOutputStream continues to be recommended for interprocess communication and general purpose serialization.

The 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:

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:

For more information you might also want to check out Using XMLEncoder, an article in The Swing Connection.

Since:
1.4
See Also:
XMLDecoder sample code for java.beans.XMLDecoder definition code for java.beans.XMLDecoder , ObjectOutputStream sample code for java.io.ObjectOutputStream definition code for java.io.ObjectOutputStream

Constructor Summary
XMLEncoder sample code for java.beans.XMLEncoder.XMLEncoder(java.io.OutputStream) definition code for java.beans.XMLEncoder.XMLEncoder(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Creates a new output stream for sending JavaBeans to the stream out using an XML encoding.
 
Method Summary
 void close sample code for java.beans.XMLEncoder.close() definition code for java.beans.XMLEncoder.close() ()
          This method calls flush, writes the closing postamble and then closes the output stream associated with this stream.
 void flush sample code for java.beans.XMLEncoder.flush() definition code for java.beans.XMLEncoder.flush() ()
          This method writes out the preamble associated with the XML encoding if it has not been written already and then writes out all of the values that been written to the stream since the last time flush was called.
 Object sample code for java.lang.Object definition code for java.lang.Object getOwner sample code for java.beans.XMLEncoder.getOwner() definition code for java.beans.XMLEncoder.getOwner() ()
          Gets the owner of this encoder.
 void setOwner sample code for java.beans.XMLEncoder.setOwner(java.lang.Object) definition code for java.beans.XMLEncoder.setOwner(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  owner)
          Sets the owner of this encoder to owner.
 void writeExpression sample code for java.beans.XMLEncoder.writeExpression(java.beans.Expression) definition code for java.beans.XMLEncoder.writeExpression(java.beans.Expression) (Expression sample code for java.beans.Expression definition code for java.beans.Expression  oldExp)
          Records the Expression so that the Encoder will produce the actual output when the stream is flushed.
 void writeObject sample code for java.beans.XMLEncoder.writeObject(java.lang.Object) definition code for java.beans.XMLEncoder.writeObject(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Write an XML representation of the specified object to the output.
 void writeStatement sample code for java.beans.XMLEncoder.writeStatement(java.beans.Statement) definition code for java.beans.XMLEncoder.writeStatement(java.beans.Statement) (Statement sample code for java.beans.Statement definition code for java.beans.Statement  oldStm)
          Records the Statement so that the Encoder will produce the actual output when the stream is flushed.
 
Methods inherited from class java.beans.Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder
get sample code for java.beans.Encoder.get(java.lang.Object) definition code for java.beans.Encoder.get(java.lang.Object) , getExceptionListener sample code for java.beans.Encoder.getExceptionListener() definition code for java.beans.Encoder.getExceptionListener() , getPersistenceDelegate sample code for java.beans.Encoder.getPersistenceDelegate(java.lang.Class) definition code for java.beans.Encoder.getPersistenceDelegate(java.lang.Class) , remove sample code for java.beans.Encoder.remove(java.lang.Object) definition code for java.beans.Encoder.remove(java.lang.Object) , setExceptionListener sample code for java.beans.Encoder.setExceptionListener(java.beans.ExceptionListener) definition code for java.beans.Encoder.setExceptionListener(java.beans.ExceptionListener) , setPersistenceDelegate sample code for java.beans.Encoder.setPersistenceDelegate(java.lang.Class, java.beans.PersistenceDelegate) definition code for java.beans.Encoder.setPersistenceDelegate(java.lang.Class, java.beans.PersistenceDelegate)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

XMLEncoder sample code for java.beans.XMLEncoder(java.io.OutputStream) definition code for java.beans.XMLEncoder(java.io.OutputStream)

public XMLEncoder(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Creates a new output stream for sending JavaBeans to the stream out using an XML encoding.

Parameters:
out - The stream to which the XML representation of the objects will be sent.
See Also:
XMLDecoder.XMLDecoder(InputStream) sample code for java.beans.XMLDecoder.XMLDecoder(java.io.InputStream) definition code for java.beans.XMLDecoder.XMLDecoder(java.io.InputStream)
Method Detail

setOwner sample code for java.beans.XMLEncoder.setOwner(java.lang.Object) definition code for java.beans.XMLEncoder.setOwner(java.lang.Object)

public void setOwner(Object sample code for java.lang.Object definition code for java.lang.Object  owner)
Sets the owner of this encoder to owner.

Parameters:
owner - The owner of this encoder.
See Also:
getOwner() sample code for java.beans.XMLEncoder.getOwner() definition code for java.beans.XMLEncoder.getOwner()

getOwner sample code for java.beans.XMLEncoder.getOwner() definition code for java.beans.XMLEncoder.getOwner()

public Object sample code for java.lang.Object definition code for java.lang.Object  getOwner()
Gets the owner of this encoder.

Returns:
The owner of this encoder.
See Also:
setOwner(java.lang.Object) sample code for java.beans.XMLEncoder.setOwner(java.lang.Object) definition code for java.beans.XMLEncoder.setOwner(java.lang.Object)

writeObject sample code for java.beans.XMLEncoder.writeObject(java.lang.Object) definition code for java.beans.XMLEncoder.writeObject(java.lang.Object)

public void writeObject(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Write an XML representation of the specified object to the output.

Overrides:
writeObject sample code for java.beans.Encoder.writeObject(java.lang.Object) definition code for java.beans.Encoder.writeObject(java.lang.Object) in class Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder
Parameters:
o - The object to be written to the stream.
See Also:
XMLDecoder.readObject() sample code for java.beans.XMLDecoder.readObject() definition code for java.beans.XMLDecoder.readObject()

writeStatement sample code for java.beans.XMLEncoder.writeStatement(java.beans.Statement) definition code for java.beans.XMLEncoder.writeStatement(java.beans.Statement)

public void writeStatement(Statement sample code for java.beans.Statement definition code for java.beans.Statement  oldStm)
Records the Statement so that the Encoder will produce the actual output when the stream is flushed.

This method should only be invoked within the context of initializing a persistence delegate.

Overrides:
writeStatement sample code for java.beans.Encoder.writeStatement(java.beans.Statement) definition code for java.beans.Encoder.writeStatement(java.beans.Statement) in class Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder
Parameters:
oldStm - The statement that will be written to the stream.
See Also:
PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder) sample code for java.beans.PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder) definition code for java.beans.PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder)

writeExpression sample code for java.beans.XMLEncoder.writeExpression(java.beans.Expression) definition code for java.beans.XMLEncoder.writeExpression(java.beans.Expression)

public void writeExpression(Expression sample code for java.beans.Expression definition code for java.beans.Expression  oldExp)
Records the Expression so that the Encoder will produce the actual output when the stream is flushed.

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

Overrides:
writeExpression sample code for java.beans.Encoder.writeExpression(java.beans.Expression) definition code for java.beans.Encoder.writeExpression(java.beans.Expression) in class Encoder sample code for java.beans.Encoder definition code for java.beans.Encoder
Parameters:
oldExp - The expression that will be written to the stream.
See Also:
PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder) sample code for java.beans.PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder) definition code for java.beans.PersistenceDelegate.initialize(java.lang.Class, java.lang.Object, java.lang.Object, java.beans.Encoder)

flush sample code for java.beans.XMLEncoder.flush() definition code for java.beans.XMLEncoder.flush()

public void flush()
This method writes out the preamble associated with the XML encoding if it has not been written already and then writes out all of the values that been written to the stream since the last time flush was called. After flushing, all internal references to the values that were written to this stream are cleared.


close sample code for java.beans.XMLEncoder.close() definition code for java.beans.XMLEncoder.close()

public void close()
This method calls flush, writes the closing postamble and then closes the output stream associated with this stream.