The xmlencoder class is a complementary replacement of objectoutputstream and can be used to generate the text representation of the JavaBean. It is used in the same way as the binary representation used to create a serializable object using objectoutputstream.
The xmldecoder class is used to read XML documents created using xmlencoder. Its purpose is similar to objectinputstream. For example, you can use the followingCodeSegment to read the first object defined in XML format (written through the xmlencoder class:
In this example, create a jframe, use xmlencoder to save the jframe object to the XML file, read the saved XML file through the xmldecoder object, and display the jframe.
Import Java. AWT. borderlayout; import Java. AWT. dimension; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. beans. xmldecoder; import Java. beans. xmlencoder; import Java. io. bufferedinputstream; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import javax. swing. abstractlistmodel; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlist; import javax. swing. jscrollpane; public class jbenchframe extends jframe {borderlayout borderlayout1 = new borderlayout (); jlist list1 = new jlist (); jbutton fillbutton = new jbutton (); public static void main (string [] ARGs) throws filenotfoundexception {// jbenchframe Bm = new jbenchframe (); /// xmlencoder E = new xmlencoder (// new bufferedoutputstream (// new fileoutputstream ("test. XML "); // E. writeobject (BM); // E. close (); xmldecoder d = new xmldecoder (New bufferedinputstream (New fileinputstream ("test. XML "); jframe result = (jframe) d. readobject (); D. close (); result. setvisible (true);} public jbenchframe () {This. getcontentpane (). setlayout (borderlayout1); this. setsize (new dimension (400,300); fillbutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {listdata LD = new listdata (); long tmstart = system. currenttimemillis (); list1.setmodel (LD); list1.repaint (); long tmend = system. currenttimemillis (); system. out. println (tmend-tmstart) ;}}); fillbutton. settext ("fill"); this. getcontentpane (). add (New jscrollpane (list1), borderlayout. center); this. getcontentpane (). add (fillbutton, borderlayout. south); setdefaclocloseoperation (jframe. exit_on_close) ;}} class listdata extends abstractlistmodel {string [] strings = new string [10000]; Public listdata () {for (INT I = 0; I <10000; I ++) {strings [I] = "Bob" + I ;}} public int getsize () {return strings. length;} public object getelementat (INT index) {return strings [Index] ;}}