FileDocCategorySizeDatePackage
XMLDecoder.javaAPI DocJava SE 5 API7447Fri Aug 26 14:56:58 BST 2005java.beans

XMLDecoder

public class XMLDecoder extends Object
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.

see
XMLEncoder
see
java.io.ObjectInputStream
since
1.4
version
1.30 06/01/04
author
Philip Milne

Fields Summary
private InputStream
in
private Object
owner
private ExceptionListener
exceptionListener
private com.sun.beans.ObjectHandler
handler
private Reference
clref
Constructors Summary
public XMLDecoder(InputStream in)
Creates a new input stream for reading archives created by the XMLEncoder class.

param
in The underlying stream.
see
XMLEncoder#XMLEncoder(OutputStream)

 
        this(in, null); 
    
public XMLDecoder(InputStream in, Object owner)
Creates a new input stream for reading archives created by the XMLEncoder class.

param
in The underlying stream.
param
owner The owner of this stream.

 
        this(in, owner, null); 
    
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)
Creates a new input stream for reading archives created by the XMLEncoder class.

param
in the underlying stream.
param
owner the owner of this stream.
param
exceptionListener the exception handler for the stream; if null the default exception listener will be used.

 
	this(in, owner, exceptionListener, null);
    
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)
Creates a new input stream for reading archives created by the XMLEncoder class.

param
in the underlying stream. null may be passed without error, though the resulting XMLDecoder will be useless
param
owner the owner of this stream. null is a legal value
param
exceptionListener the exception handler for the stream, or null to use the default
param
cl the class loader used for instantiating objects. null indicates that the default class loader should be used
since
1.5

 
        this.in = in;  
        setOwner(owner);  
        setExceptionListener(exceptionListener); 
        setClassLoader(cl);
    
Methods Summary
public voidclose()
This method closes the input stream associated with this stream.

 
        if (in != null) {
            try { 
                in.close(); 
            } 
            catch (IOException e) { 
                getExceptionListener().exceptionThrown(e); 
            }
        }
    
private java.lang.ClassLoadergetClassLoader()
Return the class loader used to instantiate objects. If the class loader has not been explicitly set then null is returned.

return
the class loader used to instantiate objects

 
        if (clref != null) {
            return (ClassLoader)clref.get();
        }
        return null; 
    
public java.beans.ExceptionListenergetExceptionListener()
Gets the exception handler for this stream.

return
The exception handler for this stream. Will return the default exception listener if this has not explicitly been set.
see
#setExceptionListener

 
        return (exceptionListener != null) ? exceptionListener : 
	    Statement.defaultExceptionListener;
    
public java.lang.ObjectgetOwner()
Gets the owner of this decoder.

return
The owner of this decoder.
see
#setOwner

	return owner; 
    
public java.lang.ObjectreadObject()
Reads the next object from the underlying input stream.

return
the next object read
throws
ArrayIndexOutOfBoundsException if the stream contains no objects (or no more objects)
see
XMLEncoder#writeObject

 
	if (in == null) {
	    return null;
	}
        if (handler == null) {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            try {
                SAXParser saxParser = factory.newSAXParser();
                handler = new ObjectHandler(this, getClassLoader());
                saxParser.parse(in, handler); 
            } 
            catch (ParserConfigurationException e) {
                getExceptionListener().exceptionThrown(e);
            } 
            catch (SAXException se) { 
                Exception e = se.getException(); 
                getExceptionListener().exceptionThrown((e == null) ? se : e); 
            }
            catch (IOException ioe) { 
                getExceptionListener().exceptionThrown(ioe); 
            }
        }
        return handler.dequeueResult(); 
    
private voidsetClassLoader(java.lang.ClassLoader cl)
Set the class loader used to instantiate objects for this stream.

param
cl a classloader to use; if null then the default class loader will be used

	if (cl != null) {
	    this.clref = new WeakReference(cl);
	}
    
public voidsetExceptionListener(java.beans.ExceptionListener exceptionListener)
Sets the exception handler for this stream to exceptionListener. The exception handler is notified when this stream catches recoverable exceptions.

param
exceptionListener The exception handler for this stream; if null the default exception listener will be used.
see
#getExceptionListener

 
        this.exceptionListener = exceptionListener; 
    
public voidsetOwner(java.lang.Object owner)
Sets the owner of this decoder to owner.

param
owner The owner of this decoder.
see
#getOwner

 
        this.owner = owner;