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.
The following is a reference fragment:
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 </title>
using XML files to access serializable objects
* <description> provides 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 the Serializable interface) to the XML file, and if you want to save multiple serializable objects at once, use the collection for encapsulation
* will be saved with the current object's original XML file content
* @param obj serializable object to serialize
* @param filename with a full save path
* @throws filenotfoundexception the file at the specified location does not exist
An exception occurred
* @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);
//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 file output stream
FileOutputStream fos = new FileOutputStream (FO);
//Create XML File object output class instance
Xmlencoder encoder = new Xmlencoder (FOS);
//object serialization output to XML file
encoder.writeobject (obj);
Encoder.flush ();
//Turn off serialization tools
Encoder.close ();
//Turn off output stream
Fos.close ();
}
/**
* Reads the serialized saved object in the XML file specified by Objsource, and returns the result after the list package
* @param objsource full file path with full file
* @return List of objects that are stored in an XML file (possibly one or more serialized objects)
* @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;
}
}