xml格式字符串与java对象互转


import java.lang.reflect.Field;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class XmlUtil {
// 所有需要注册的类的配置,
public static final String CONFIGFILE = new GetClassPath()
.getMyConfigPath()
+ "/xml.prop";

private static XStream xs = null;

private static XmlUtil xmlUtil = null;

private XmlUtil() {
try {
// 获取所有需要与处理的java完整名
List list = FileUtil.readFile(CONFIGFILE);

//初始化xs
xs = alias(list);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* 获得实例
* @return
*/
public static synchronized XmlUtil getInstance() {

if(null == xmlUtil){
xmlUtil = new XmlUtil();
}

return xmlUtil;
}

/**
* 根据类名做反射
*
* @param list
* 类完整名
* @return
* @throws ClassNotFoundException
*/
public XStream alias(List list) throws ClassNotFoundException {
XStream xs = new XStream();
for (int i = 0; i < list.size(); i++) {
Object obj;
try {
obj = Class.forName((String) list.get(i)).newInstance();
Class zz = obj.getClass();
aliasAtt(xs, zz);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return xs;
}

/**
* 对类进行xml解析配置
*
* @param xs
* @param z
* class对象
*/
public void aliasAtt(XStream xs, Class z) {
if (null == z) {
return;
}

// 类名注册
xs.alias(z.getSimpleName(), z);
Field[] field = z.getDeclaredFields();
// 类中成员变量注册,继承的不注册
for (int i = 0; i < field.length; i++) {
xs.aliasField(field[i].getName(), z, field[i].getName());
}
}

/**
* xml 格式字符串转换为对象
* @param str
* @return
*/
public Object xmlToObject(String str) {
return xs.fromXML(str);
}

/**
* 对象转换为xml格式字符串
* @param obj
* @return
*/
public String objToXml(Object obj) {
return xs.toXML(obj);
}
}

FileUtil.readFile()方法如下

/**
* 读取文件
*
* @param filePath
* 文件完整名
* @return 文件内容
* @throws IOException
*/
public static List readFile(String filePath) {

List list = new ArrayList();

FileInputStream fs = null;
InputStreamReader isr = null;
BufferedReader br = null;

try {
fs = new FileInputStream(filePath);

isr = new InputStreamReader(fs);

br = new BufferedReader(isr);

String data = "";
while ((data = br.readLine()) != null) {
list.add(data.trim());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();

if (fs != null)
fs.close();

} catch (IOException e) {
e.printStackTrace();
}
}

return list;
}

Test
Printdata pd = new Printdata();
pd.setPrintFileList(printFileList);
pd.setAuto("1");
pd.setBillReceiver("billReceiver");
pd.setCustId("cust_id");
pd.setYear("2011");
pd.setMonth("11");
List list = FileUtil.readFile(XmlUtil.CONFIGFILE);
XmlUtil xmlUtil = XmlUtil.getInstance();
String xml = xmlUtil.objToXml(pd);//对象至xml
System.out.println(xml);
Printdata obj = (Printdata)xmlUtil.xmlToObject(xml);//xml至对象

需要的jar包见附件



[list]
[*][url=http://dl.iteye.com/topics/download/a075ccd6-0fed-37ef-96fe-fcc6e072d721]xpp3_min-1.1.4c.jar[/url] (24.4 KB)


[*]下载次数: 1

[/list]



[list]
[*][url=http://dl.iteye.com/topics/download/e2a36f71-de3b-336a-8c23-6a2f4fb9e318]xstream-1.3.jar[/url] (401.5 KB)


[*]下载次数: 1

[/list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值