Package j2se.core.base; import java.beans.xmldecoder;import java.beans.xmlencoder;import java.io.*;import java.util.ArrayList;import java.util.List; /** * Created by lenovo on 2015/11/26. */public class xmlutil { /* * * reads the serialized saved object in the XML file specified by Objsource, and the returned results are in the list package * * @param objSource * full file path file name * @ return a list of objects that are stored in an XML file (possibly one or more serialized saved objects) * @throws java.io.filenotfoundexception * the specified object read resource does not exist * @throws java.io.ioexception * Read error occurred * @throws Exception * Other run-time exceptions occur */ Public static list objectxmldecoder (String objsource) throws filenotfoundexception, ioexception, exception { List objlist = new arraylist (); file Fin = new file (Objsource); fileinputstream Fis = new fileinputstream (Fin); xmldecoder Decoder = new xmldecoder (FIS); object obj = null; try { while ((Obj = decoder.readobject ()) != null) { objlist.add (obj); } } catch (exception e) { } fis.close (); decoder.close (); return objList; } /** * save a collection to an XML file * @param objects * @param fileName * @throws ioexception &nbSp; */ public static void objectxmlencoderlist (List Objects,string filename) throws IOException { // Create an output file file fo = new file ( FileName); // file does not exist, create the file if (!fo.exists ()) { // Create a directory of files first string path = filename.substring (0, filename.lastindexof ('. ')); File pFile = new File (path); pfile.mkdirs (); } // creating a file output stream fileoutputstream fos = new fileoutputstream (FO); // Create an XML file object output class instance XMLEncoder encoder = new Xmlencoder (FOS); // object serialized output to XML file for (object obj : objects) { encoder.writeobject (obj); } encoder.flush (); // Close the serialization Tool encoder.close (); // off output stream fos.close (); } /** * serializes a Java serializable object (implementing the Serializable interface) into an XML file, If you want to save more than one serializable object at a time, encapsulate save with a collection will use the current object's original XML file content * * @param obj * serializable objects to serialize * @param fileName * file name with full save path * @throws FileNotFoundException * the file at the specified location does not exist * @throws IOException * exception occurred * @throws on output Exception * Other run-time exceptions */ public static void objectxmlencoder (Object obj, string filename) throws FileNotFoundException, IOException, Exception { // Create an output file file fo = new file (fileName); // file does not exist, create the file if (!fo.exists ()) { // Create a directory of files first string path = filename.substring (0, filename.lastindexof ('. ')); File pFile = new File (path); pfile.mkdirs (); } // Create a file output stream Fileoutputstream fos = new fileoutputstream (FO); // create an XML file object output class instance xmlencoder encoder = new xmlencoder (FOS); // object serialized output to XML file encoder.writeobject (obj); encoder.flush (); // Turn off serialization tool encoder.close (); // off output stream fos.close (); }}
Test:
package j2se.core.base; import java.util.arraylist;import java.util.list; /** * created by lenovo on 2015/11/26. */public class xmlencodertest { public static void main (String[] args) { // using Xmlencoder to save a Java serialized object to an XML file string file = new string ("D:/test/person.xml"); List<Person> personList = new ArrayList<Person> (2); // constructs a Person object person person = new person (); person.setid (100001 ); person.setname ("Zhang San"); person.seTphonenum ("15825625152"); // animal1 animal animal1 = new animal (); animal1.setid (1); animal1.setname (" Petter "); // animal2 animal animal2 = new animal (); animal2.setid (2); animal2.setname ("rhubarb"); List<Animal> animals = new ArrayList< > (2); animals.add (ANIMAL1); animals.add (Animal2); person.setanimals ( Animals); &NBSP;&NBSP;&NBSP;&NBSP;&NBsp; // person2 person person2 = new person (); person2.setid (100002); person2.setname ("History"); person2.setphonenum ("158525252551"); list<animal > animalList = new ArrayList<> (1); animal animal = new animal (); Animal.setid (3); animal.setname ("Fruit"); animallist.add (animal); Person2.setanimals (animallist); personlist.add (person); persoNlist.add (Person2); try { xmlutil.objectxmlencoderlist (PersonList, file); List<Person> persons = Xmlutil.objectxmldecoder (file); for (person p : persons) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (P); } } catch (Exception e) { e.printstacktrace (); } }}
Generated XML file:
<?xml version= "1.0" encoding= "UTF-8"? ><java version= "1.7.0_04" class= " Java.beans.XMLDecoder "> <object class=" J2se.core.base.Person "> <void property= "Animals" > <object class= "java.util.ArrayList" > <void method= "Add" > <object class= "J2se.core.base.Animal "> <void property=" id "> <int>1</int> </void> <void property= "Name" > <string>petter</ string> </void> </object> </void> <void method= "Add" > <object class= "J2se.corE.base.animal "> <void property=" id "> <int>2</int> </void> <void property= "name" > <string> Rhubarb </string> </void> </object> </void> </object> </void> <void property= "id" > <int>100001</int> </void> <void property= "Name" > <string> Zhang San </string> </void > <void property= "Phonenum" > <string>15825625152</string > </void> </object> <object class= "J2se.core.base.Person" > <void property= "Animals" > <object class= "java.util.ArrayList" > <void method= "Add" > <object class= "J2se.core.base.Animal" > <void property= "id" > <int>3</int> </void> <void property= "name" > <string> fruit </string> </void> </object> </void> </object> </void> <void property= "id" > <int>100002</int> </void> <void property= "Name" > <string> history </string> </void> <void property= "Phonenum" > <string>158525252551</string> </void> </ Object></java>
This article is from the "Technology Change Life" blog, please be sure to keep this source http://qincidong.blog.51cto.com/8116440/1717076
Java.beans Package Xmlencoder and Xmldecoder implementing entity and XML transformations