java.beans
public class: XMLDecoder [javadoc |
source]
java.lang.Object
java.beans.XMLDecoder
All Implemented Interfaces:
AutoCloseable
The
XMLDecoder
class is used to read XML documents
created using the
XMLEncoder
and is used just like
the
ObjectInputStream
. For example, one can use
the following fragment to read the first object defined
in an XML document written by the
XMLEncoder
class:
XMLDecoder d = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("Test.xml")));
Object result = d.readObject();
d.close();
For more information you might also want to check out
Long Term Persistence of JavaBeans Components: XML Schema,
an article in The Swing Connection.
Constructor: |
public XMLDecoder(InputStream in) {
this(in, null);
}
Creates a new input stream for reading archives
created by the XMLEncoder class. Parameters:
in - The underlying stream.
Also see:
- XMLEncoder#XMLEncoder(java.io.OutputStream)
|
public XMLDecoder(InputSource is) {
this(is, null, null, null);
}
Creates a new decoder to parse XML archives
created by the {@code XMLEncoder} class.
If the input source {@code is} is {@code null},
no exception is thrown and no parsing is performed.
This behavior is similar to behavior of other constructors
that use {@code InputStream} as a parameter. Parameters:
is - the input source to parse
- since:
1.7 -
|
public XMLDecoder(InputStream in,
Object owner) {
this(in, owner, null);
}
Creates a new input stream for reading archives
created by the XMLEncoder class. Parameters:
in - The underlying stream.
owner - The owner of this stream.
|
public XMLDecoder(InputStream in,
Object owner,
ExceptionListener exceptionListener) {
this(in, owner, exceptionListener, null);
}
Creates a new input stream for reading archives
created by the XMLEncoder class. Parameters:
in - the underlying stream.
owner - the owner of this stream.
exceptionListener - the exception handler for the stream;
if null the default exception listener will be used.
|
public XMLDecoder(InputStream in,
Object owner,
ExceptionListener exceptionListener,
ClassLoader cl) {
this(new InputSource(in), owner, exceptionListener, cl);
}
Creates a new input stream for reading archives
created by the XMLEncoder class. Parameters:
in - the underlying stream. null may be passed without
error, though the resulting XMLDecoder will be useless
owner - the owner of this stream. null is a legal
value
exceptionListener - the exception handler for the stream, or
null to use the default
cl - the class loader used for instantiating objects.
null indicates that the default class loader should
be used
- since:
1.5 -
|
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from java.beans.XMLDecoder Detail: |
public void close() {
if (parsingComplete()) {
close(this.input.getCharacterStream());
close(this.input.getByteStream());
}
}
This method closes the input stream associated
with this stream. |
public static DefaultHandler createHandler(Object owner,
ExceptionListener el,
ClassLoader cl) {
DocumentHandler handler = new DocumentHandler();
handler.setOwner(owner);
handler.setExceptionListener(el);
handler.setClassLoader(cl);
return handler;
}
Creates a new handler for SAX parser
that can be used to parse embedded XML archives
created by the {@code XMLEncoder} class.
The {@code owner} should be used if parsed XML document contains
the method call within context of the <java> element.
The {@code null} value may cause illegal parsing in such case.
The same problem may occur, if the {@code owner} class
does not contain expected method to call. See details here. |
public ExceptionListener getExceptionListener() {
return this.handler.getExceptionListener();
}
Gets the exception handler for this stream. |
public Object getOwner() {
return owner;
}
Gets the owner of this decoder. |
public Object readObject() {
return (parsingComplete())
? this.array[this.index++]
: null;
}
Reads the next object from the underlying input stream. |
public void setExceptionListener(ExceptionListener exceptionListener) {
if (exceptionListener == null) {
exceptionListener = Statement.defaultExceptionListener;
}
this.handler.setExceptionListener(exceptionListener);
}
Sets the exception handler for this stream to exceptionListener .
The exception handler is notified when this stream catches recoverable
exceptions. |
public void setOwner(Object owner) {
this.owner = owner;
}
Sets the owner of this decoder to owner . |