加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml转换操作类

发布时间:2020-12-16 05:07:07 所属栏目:百科 来源:网络整理
导读: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
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);
		
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读