xml转换操作类

package com.*.util.xml;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class XmlOperate {

	// 读取xml文件
	public static String readFile(String fileName) {
		// TODO Auto-generated method stub
		StringBuffer requestXML = new StringBuffer();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileName),"utf-8"));
			String s = null;
			while ((s = reader.readLine()) != null) {
				requestXML.append(s);
			}
			reader.close();
		} catch (IOException ex) {

		}
		return requestXML.toString();
	}

	// 写入xml文件
	public static void writeFile(String fileName,String responseString) {
		// TODO Auto-generated method stub
		try {
			OutputStreamWriter out = new OutputStreamWriter(
					new FileOutputStream(fileName),"UTF-8");
			out.write(responseString);
			out.flush();
			out.close();
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
		}
	}

	// 根据Java对象构建Xml
	public static String Object2Xml(Object object) {
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-","_")));
		xStream.processAnnotations(object.getClass());
		return xStream.toXML(object);

	}

	// 根据List对象构建Xml
	public static String List2Xml(List<String> list,String root,String str) {
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-","_")));
		xStream.processAnnotations(list.getClass());
		xStream.alias(root,List.class);
		xStream.alias(str,String.class);
		return xStream.toXML(list);
	}

	// 根据List对象构建Xml
	public static String List2Xml(List<String> list,String root) {
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-",List.class);
		return xStream.toXML(list);
	}

	// 根据Xml生成Java对象
	public static Object Xml2Object(String xmlString,Class<?> clazz) {
		if (xmlString == null || "".equals(xmlString.trim())) {
			return null;
		}

		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-","_")));
		Object rs = null;
		xStream.processAnnotations(clazz);
		xStream.autodetectAnnotations(true);
		System.out.println("xmlString==="+xmlString);
		rs = xStream.fromXML(xmlString);
		return rs;
	}
	
	public static XStream getXStream() {

		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-","_")));
		return xStream;
	}
	

	// 根据Xml生成List对象
	public static <T> List<T> Xml2List(String xmlString,Class<T> clazz) {
		if (xmlString == null || "".equals(xmlString.trim())) {
			return null;
		}
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(
				"_-","_")));
		xStream.alias(root,List.class);
		xStream.processAnnotations(clazz);
		List<T> rs = (List<T>) xStream.fromXML(xmlString);
		return rs;
	}

	// 根据Xml生成List对象
	public static <T> List<T> Xml2List(String xmlString,Class<T> clazz) {
		return Xml2List(xmlString,"root",clazz);
	}

	public static void main(String[] args) {
		
	//	List<RDBConnectModel> list = Xml2List(xmlString,RDBConnectModel.class);
		
	}
}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...