Java Object |xml
Today I looked at the Java.beans bag and found two good things, Xmlencoder and Xmldecoder. It's hard to find yourself accessing objects from XML before. Do the gadget class, you can use it later.
Package com.imct.util;
Import Java.beans.XMLDecoder;
Import Java.beans.XMLEncoder;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
/**
* <title> classes that use XML files to access serializable objects </title>
* <description> provide methods of saving and reading </description>
* @author Yinjin
* <copyright> Institute of Automotive Engineering Development, Tsinghua University, @2005</copyright>
* @version 1.0
* 2005-8-5 16:44:49
*/
public class Objecttoxmlutil
{
/**
* Serialize Java Serializable objects (implementing serializable interfaces) to XML files, and if you want to save multiple serializable objects at once, encapsulate them with a collection
* Save will use the current object's original XML file content
* @param the Serializable object to be serialized by obj
* @param filename with a full save path
* @throws FileNotFoundException The file at the specified location does not exist
* An exception occurred while @throws IOException output
* @throws Exception Other run-time exceptions
*/
public static void Objectxmlencoder (Object obj,string fileName)
Throws Filenotfoundexception,ioexception,exception
{
Create output file
File fo = new file (fileName);
The file does not exist, the file is created
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 serialization output to an XML file
Encoder.writeobject (obj);
Encoder.flush ();
Turn off serialization tools
Encoder.close ();
Turn off the output stream
Fos.close ();
}
/**
* Read the serialized saved object in the XML file specified by Objsource, and the result returned is in the list package
* @param objsource full file name with full file path
* @return List of objects that are saved in an XML file (possibly one or more serialized objects that are saved)
* @throws FileNotFoundException The specified object read resource does not exist
* @throws IOException Read error
* @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)
{
TODO auto-generated Catch block
}
Fis.close ();
Decoder.close ();
Return objlist;
}
}