xstream list读写

/**
 * 
 */
package com.automic.commu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;

import com.automic.commu.bo.Data;
import com.automic.commu.bo.DataPackage;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
 * @author hxb
 *
 */
public class OperatorXml {

	private XStream xstream;
	
	public OperatorXml(){
		if(xstream == null){
			xstream = new XStream(new DomDriver());
		}
	} 
	
	/**
	 * @return 生成xml
	 */
	public void createXml(){
		String xmlName = System.currentTimeMillis() + ".xml";
		/********/
		DataPackage dp = new DataPackage();
		ArrayList<Data> das = new ArrayList<Data>(0);
		Data d1 = new Data();
		Data d2 = new Data();
		d1.setGprsId("12345678991");
		d1.setMeterNo("我说");
		d1.setDateTime("2009-08-14 12:00:00");
		d2.setGprsId("12345678992");
		d2.setMeterNo("1100008892");
		d2.setDateTime("2009-09-15 12:00:00");
		das.add(d1);
		das.add(d2);
		
		dp.setDatas(das);
		
		xstream.alias(DataPackage.DATAPACKAGE, DataPackage.class);
		xstream.alias(DataPackage.DATA, Data.class);
		try {
			FileOutputStream fos = new FileOutputStream(new File(DataPackage.XMLSAVEDIR + xmlName));
			OutputStreamWriter osr = new OutputStreamWriter(fos,Charset.forName("GBK"));
			xstream.toXML(dp, osr);
			System.out.println("文件生成完毕!!");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*******/
		
	}
	/**
	 * @param fis 读取xml
	 */
	@SuppressWarnings("deprecation")
	public void readXml(){
		File f = new File(DataPackage.XMLSAVEDIR);
		if(f != null){
			File[] fs = f.listFiles();
			for(int i = 0;fs != null && i < fs.length;i++){
				File ftemp = fs[i];
				System.out.println(ftemp.getName());
				xstream.alias(DataPackage.DATAPACKAGE, DataPackage.class);
				xstream.alias(DataPackage.DATA, Data.class);
				try {
					DataPackage dp = (DataPackage)xstream.fromXML(new InputStreamReader(new FileInputStream(ftemp),Charset.forName("GBK")));
					ArrayList<Data> das = dp.getDatas();
					for(int k = 0;das != null && k < das.size();k++){
						Data da = das.get(k);
						System.out.println(da.getGprsId() + "-" + da.getMeterNo() + "-" + da.getDateTime());
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * @param args0
	 */
	public static void main(String[] args0){
		OperatorXml tx = new OperatorXml();
//		tx.createXml();
		tx.readXml();
	}
	
	/**
	 * @author hxb
	 * 
	 */
	
}

 

/**
 * 
 */
package com.automic.commu.bo;

import java.util.ArrayList;

/**
 * @author hxb
 *
 */
public class DataPackage {

	public final static String DATAPACKAGE = "dataPackage";
	public final static String DATA = "data";
	public final static String XMLSAVEDIR = ".\\xml\\";
	
	private ArrayList<Data> datas;

	public ArrayList<Data> getDatas() {
		return datas;
	}

	public void setDatas(ArrayList<Data> datas) {
		this.datas = datas;
	}
}

 

/**
 * 
 */
package com.automic.commu.bo;

/**
 * @author hxb
 *
 */
public class Data {

	private String gprsId;
	
	private String meterNo;
	
	private String dateTime;

	public String getDateTime() {
		return dateTime;
	}

	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}

	public String getGprsId() {
		return gprsId;
	}

	public void setGprsId(String gprsId) {
		this.gprsId = gprsId;
	}

	public String getMeterNo() {
		return meterNo;
	}

	public void setMeterNo(String meterNo) {
		this.meterNo = meterNo;
	}
	
}

 

你可能感兴趣的:(java,xml,F#)