XMLDecoder (Java SE 21 & JDK 21)
- 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.
- Since:
- 1.4
- See Also:
-
Constructor Summary
Constructors
Creates a new input stream for reading archives created by the
XMLEncoderclass.Creates a new input stream for reading archives created by the
XMLEncoderclass.Creates a new input stream for reading archives created by the
XMLEncoderclass.Creates a new input stream for reading archives created by the
XMLEncoderclass.Creates a new decoder to parse XML archives created by the
XMLEncoderclass. -
Method Summary
voidclose()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
XMLEncoderclass.Gets the exception handler for this stream.
getOwner()Gets the owner of this decoder.
Reads the next object from the underlying input stream.
voidSets the exception handler for this stream to
exceptionListener.voidSets the owner of this decoder to
owner.
-
Constructor Details
-
XMLDecoder
Creates a new input stream for reading archives created by the
XMLEncoderclass.- Parameters:
in- The underlying stream.- See Also:
-
XMLDecoder
Creates a new input stream for reading archives created by the
XMLEncoderclass.- Parameters:
in- The underlying stream.owner- The owner of this stream.
-
XMLDecoder
Creates a new input stream for reading archives created by the
XMLEncoderclass.- Parameters:
in- the underlying stream.owner- the owner of this stream.exceptionListener- the exception handler for the stream; ifnullthe default exception listener will be used.
-
XMLDecoder
Creates a new input stream for reading archives created by the
XMLEncoderclass.- Parameters:
in- the underlying stream.nullmay be passed without error, though the resulting XMLDecoder will be uselessowner- the owner of this stream.nullis a legal valueexceptionListener- the exception handler for the stream, ornullto use the defaultcl- the class loader used for instantiating objects.nullindicates that the default class loader should be used- Since:
- 1.5
-
XMLDecoder
Creates a new decoder to parse XML archives created by the
XMLEncoderclass. If the input sourceisisnull, no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that useInputStreamas 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:
closein interfaceAutoCloseable
-
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; ifnullthe default exception listener will be used.- See Also:
-
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
Gets the owner of this decoder.
- Returns:
- The owner of this decoder.
- See Also:
-
createHandler
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the
XMLEncoderclass. Theownershould be used if parsed XML document contains the method call within context of the <java> element. Thenullvalue may cause illegal parsing in such case. The same problem may occur, if theownerclass 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> elementel- the exception handler for the parser, ornullto use the default exception handlercl- the class loader used for instantiating objects, ornullto use the default class loader- Returns:
- an instance of
DefaultHandlerfor SAX parser - Since:
- 1.7
-