xml转换操作类

前端之家收集整理的这篇文章主要介绍了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);
		
	}
}

猜你在找的XML相关文章