Java tool class for reading XML files

wrote
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 for accessing serializable objects using XML files</title>
* <description>Provide storage and Reading method</description>
* @author Yin Jin
* <copyright>Automotive Engineering Development Research Institute of Tsinghua University@2005</copyright>
* @version 1.0
* 2005-8-5 16:44:49
*/
public class ObjectToXMLUtil
{
/**
* Serialize and save java serializable objects (implementing the Serializable interface) into an XML file. If you want to save multiple serializable objects at a time, please encapsulate them with a collection.
* When saving, the original XML file of the current object will be used content
* @param obj Serializable object to be serialized
* @param fileName filename with full save path
* @throws FileNotFoundException file at specified location does not exist
* @throws IOException exception occurred while outputting
* @throws Exception other run Time exception
*/
public static void objectXmlEncoder(Object obj,String fileName)
throws FileNotFoundException,IOException,Exception
{
//Create an output file
File fo = new File(fileName);
//If the file does not exist, create the file
if(!fo .exists())
{
//Create the directory of the file 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();
//Close the serialization tool
encoder.close();
//Close the output stream
fos.close();
}
/**
* Read the XML file specified by objSource Serialize the saved object, and the returned result is encapsulated by List
* @param objSource file full name with all file paths
* @return List composed of objects saved in the XML file (may be one or more serialized saves object)
* @throws FileNotFoundException specified object read resource does not exist
* @throws IOException read error
* @throws Exception other runtime exception occurs
*/
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;
}
}

 

 http://www.myexception.cn/xml-soap/756039.html

 

 

http://www.blogjava.net/mlh123caoer/articles/26913.html

http://blog.csdn.net/lanzhizhuxia/article/details/7941655

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326771667&siteId=291194637