Class XMLDecoder

java.lang.Object
java.beans.XMLDecoder
All Implemented Interfaces:
AutoCloseable

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

Since:
1.4
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new input stream for reading archives created by the XMLEncoder class.
    Creates a new input stream for reading archives created by the XMLEncoder class.
    XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)
    Creates a new input stream for reading archives created by the XMLEncoder class.
    XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)
    Creates a new input stream for reading archives created by the XMLEncoder class.
    Creates a new decoder to parse XML archives created by the XMLEncoder class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method closes the input stream associated with this stream.
    Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class.
    Gets the exception handler for this stream.
    Gets the owner of this decoder.
    Reads the next object from the underlying input stream.
    void
    Sets the exception handler for this stream to exceptionListener.
    void
    Sets the owner of this decoder to owner.

    Methods declared in class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
    Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.
    boolean
    Compares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    May cause performance issues, deadlocks and hangs.
    final Class<? extends Object>
    Answers the unique instance of java.lang.Class which represents the class of the receiver.
    int
    Answers an integer hash code for the receiver.
    final void
    Causes one thread which is waiting on the receiver to be made ready to run.
    final void
    Causes all threads which are waiting on the receiver to be made ready to run.
    Answers a string containing a concise, human-readable description of the receiver.
    final void
    Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated by notify or notifyAll).
    final void
    wait(long time)
    Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.
    final void
    wait(long time, int frac)
    Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated by notify or notifyAll) or the expiration of the timeout.
  • Constructor Details

    • XMLDecoder

      public XMLDecoder(InputStream in)
      Creates a new input stream for reading archives created by the XMLEncoder class.
      Parameters:
      in - The underlying stream.
      See Also:
    • XMLDecoder

      public XMLDecoder(InputStream in, Object owner)
      Creates a new input stream for reading archives created by the XMLEncoder class.
      Parameters:
      in - The underlying stream.
      owner - The owner of this stream.
    • XMLDecoder

      public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)
      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.
    • XMLDecoder

      public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader 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
    • XMLDecoder

      public XMLDecoder(InputSource is)
      Creates a new decoder to parse XML archives created by the XMLEncoder class. If the input source is is null, no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that use InputStream as a parameter.
      Parameters:
      is - the input source to parse
      Since:
      1.7
  • Method Details

    • close

      public void close()
      This method closes the input stream associated with this stream.
      Specified by:
      close in interface AutoCloseable
    • setExceptionListener

      public void setExceptionListener(ExceptionListener exceptionListener)
      Sets the exception handler for this stream to exceptionListener. The exception handler is notified when this stream catches recoverable exceptions.
      Parameters:
      exceptionListener - The exception handler for this stream; if null the default exception listener will be used.
      See Also:
    • getExceptionListener

      public ExceptionListener getExceptionListener()
      Gets the exception handler for this stream.
      Returns:
      The exception handler for this stream. Will return the default exception listener if this has not explicitly been set.
      See Also:
    • readObject

      public Object readObject()
      Reads the next object from the underlying input stream.
      Returns:
      the next object read
      Throws:
      ArrayIndexOutOfBoundsException - if the stream contains no objects (or no more objects)
      See Also:
    • setOwner

      public void setOwner(Object owner)
      Sets the owner of this decoder to owner.
      Parameters:
      owner - The owner of this decoder.
      See Also:
    • getOwner

      public Object getOwner()
      Gets the owner of this decoder.
      Returns:
      The owner of this decoder.
      See Also:
    • createHandler

      public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl)
      Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class. The owner should be used if parsed XML document contains the method call within context of the <java> element. The null value may cause illegal parsing in such case. The same problem may occur, if the owner class does not contain expected method to call. See details here.
      Parameters:
      owner - the owner of the default handler that can be used as a value of <java> element
      el - the exception handler for the parser, or null to use the default exception handler
      cl - the class loader used for instantiating objects, or null to use the default class loader
      Returns:
      an instance of DefaultHandler for SAX parser
      Since:
      1.7