APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.beans

public Class XMLEncoder

extends Encoder
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Imports
java.io.*, java.util.*, java.lang.reflect.*, java.nio.charset.Charset, .CharsetEncoder, .IllegalCharsetNameException, .UnsupportedCharsetException

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.

Author
Philip Milne
Since
1.4
See Also
XMLDecoder, java.io.ObjectOutputStream

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final String
private final boolean
private final CharsetEncoder
private int
private boolean
private NameGenerator
private OutputStreamWriter
private Object
private boolean
private Map<Object, List<Statement>>
private Map<Object, XMLEncoder.ValueData>
Inherited from java.beans.Encoder:
executeStatements

Constructor Summary

AccessConstructor and Description
public
XMLEncoder(OutputStream
the stream to which the XML representation of the objects will be written
out
)

Creates a new XML encoder to write out JavaBeans to the stream out using an XML encoding.

public
XMLEncoder(OutputStream
the stream to which the XML representation of the objects will be written
out
,
String
the name of the requested charset; may be either a canonical name or an alias
charset
,
boolean
whether the XML declaration should be generated; set this to false when embedding the contents in another XML document
declaration
,
int
the number of space characters to indent the entire XML document by
indentation
)

Creates a new XML encoder to write out JavaBeans to the stream out using the given charset starting from the given indentation.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public void
close()

Implements java.lang.AutoCloseable.close.

This method calls flush, writes the closing postamble and then closes the output stream associated with this stream.

private static String
createString(int code)

private String
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.

pack-priv Statement
public Object

Returns:

The owner of this encoder.
getOwner
()

Gets the owner of this encoder.

private XMLEncoder.ValueData
private static boolean

Returns:

true if the Unicode code point is valid, false otherwise
isValidCharCode
(int
the 32-bit Unicode code point being tested
code
)

Returns true if the argument, a Unicode code point, is valid in XML documents.</par>

private void
mark(Object o, boolean isArgument)

private void
private void
outputStatement(Statement exp, Object outer, boolean isArgument)

private void
outputValue(Object value, Object outer, boolean isArgument)

private void
outputXML(String tag, String attributes, Object value, Object... args)

pack-priv static Class<T>
primitiveTypeFor(Class<T> wrapper)

private String
private static String
quoteCharCode(int code)

public void
setOwner(Object
The owner of this encoder.
owner
)

Sets the owner of this encoder to owner.

private List<Statement>
public void
writeExpression(Expression
The expression that will be written to the stream.
oldExp
)

Overrides java.beans.Encoder.writeExpression.

Records the Expression so that the Encoder will produce the actual output when the stream is flushed.

private void
public void
writeObject(Object
The object to be written to the stream.
o
)

Overrides java.beans.Encoder.writeObject.

Write an XML representation of the specified object to the output.

public void
writeStatement(Statement
The statement that will be written to the stream.
oldStm
)

Overrides java.beans.Encoder.writeStatement.

Records the Statement so that the Encoder will produce the actual output when the stream is flushed.

Inherited from java.beans.Encoder:
getgetAttributegetExceptionListenergetPersistenceDelegategetValueremovesetAttributesetExceptionListenersetPersistenceDelegate

Field Detail

charsetback to summary
private final String charset
declarationback to summary
private final boolean declaration
encoderback to summary
private final CharsetEncoder encoder
indentationback to summary
private int indentation
internalback to summary
private boolean internal
nameGeneratorback to summary
private NameGenerator nameGenerator
outback to summary
private OutputStreamWriter out
ownerback to summary
private Object owner
preambleWrittenback to summary
private boolean preambleWritten
targetToStatementListback to summary
private Map<Object, List<Statement>> targetToStatementList
valueToExpressionback to summary
private Map<Object, XMLEncoder.ValueData> valueToExpression

Constructor Detail

XMLEncoderback to summary
public XMLEncoder(OutputStream out)

Creates a new XML encoder to write out JavaBeans to the stream out using an XML encoding.

Parameters
out:OutputStream

the stream to which the XML representation of the objects will be written

Exceptions
IllegalArgumentException:
if out is null
See Also
XMLDecoder#XMLDecoder(InputStream)
XMLEncoderback to summary
public XMLEncoder(OutputStream out, String charset, boolean declaration, int indentation)

Creates a new XML encoder to write out JavaBeans to the stream out using the given charset starting from the given indentation.

Parameters
out:OutputStream

the stream to which the XML representation of the objects will be written

charset:String

the name of the requested charset; may be either a canonical name or an alias

declaration:boolean

whether the XML declaration should be generated; set this to false when embedding the contents in another XML document

indentation:int

the number of space characters to indent the entire XML document by

Exceptions
IllegalArgumentException:
if out or charset is null, or if indentation is less than 0
IllegalCharsetNameException:
if charset name is illegal
UnsupportedCharsetException:
if no support for the named charset is available in this instance of the Java virtual machine
UnsupportedOperationException:
if loaded charset does not support encoding
Since
1.7
See Also
Charset#forName(String)

Method Detail

clearback to summary
pack-priv void clear()

Overrides java.beans.Encoder.clear.

Annotations
@Override
closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

This method calls flush, writes the closing postamble and then closes the output stream associated with this stream.

Annotations
@Override
createStringback to summary
private static String createString(int code)
createStringback to summary
private String createString(String string)
flushback to summary
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.

getMissedStatementback to summary
pack-priv Statement getMissedStatement()
getOwnerback to summary
public Object getOwner()

Gets the owner of this encoder.

Returns:Object

The owner of this encoder.

See Also
setOwner
getValueDataback to summary
private XMLEncoder.ValueData getValueData(Object o)
isValidCharCodeback to summary
private static boolean isValidCharCode(int code)

Returns true if the argument, a Unicode code point, is valid in XML documents. Unicode characters fit into the low sixteen bits of a Unicode code point, and pairs of Unicode surrogate characters can be combined to encode Unicode code point in documents containing only Unicode. (The char datatype in the Java Programming Language represents Unicode characters, including unpaired surrogates.) [2] Char ::= #x0009 | #x000A | #x000D | [#x0020-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10ffff]

Parameters
code:int

the 32-bit Unicode code point being tested

Returns:boolean

true if the Unicode code point is valid, false otherwise

markback to summary
private void mark(Object o, boolean isArgument)
markback to summary
private void mark(Statement stm)
outputStatementback to summary
private void outputStatement(Statement exp, Object outer, boolean isArgument)
outputValueback to summary
private void outputValue(Object value, Object outer, boolean isArgument)
outputXMLback to summary
private void outputXML(String tag, String attributes, Object value, Object... args)
primitiveTypeForback to summary
pack-priv static Class<T> primitiveTypeFor(Class<T> wrapper)
Annotations
@SuppressWarnings:rawtypes
quoteback to summary
private String quote(String s)
quoteCharCodeback to summary
private static String quoteCharCode(int code)
setOwnerback to summary
public void setOwner(Object owner)

Sets the owner of this encoder to owner.

Parameters
owner:Object

The owner of this encoder.

See Also
getOwner
statementListback to summary
private List<Statement> statementList(Object target)
writeExpressionback to summary
public void writeExpression(Expression oldExp)

Overrides java.beans.Encoder.writeExpression.

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 Creating Internationalized Applications,

Parameters
oldExp:Expression

The expression that will be written to the stream.

Annotations
@Override
See Also
java.beans.PersistenceDelegate#initialize
writelnback to summary
private void writeln(String exp)
writeObjectback to summary
public void writeObject(Object o)

Overrides java.beans.Encoder.writeObject.

Write an XML representation of the specified object to the output.

Parameters
o:Object

The object to be written to the stream.

Annotations
@Override
See Also
XMLDecoder#readObject
writeStatementback to summary
public void writeStatement(Statement oldStm)

Overrides java.beans.Encoder.writeStatement.

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.

Parameters
oldStm:Statement

The statement that will be written to the stream.

Annotations
@Override
See Also
java.beans.PersistenceDelegate#initialize
java.beans back to summary

private Class XMLEncoder.ValueData

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public Expression
public boolean
public String
public int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

expback to summary
public Expression exp
markedback to summary
public boolean marked
nameback to summary
public String name
refsback to summary
public int refs

Constructor Detail

ValueDataback to summary
private ValueData()