java xmlutil_XmlUtil工具类(toxml()和toBean())

这是一个关于Java中XmlUtil工具类的介绍,包括如何将Java对象转换为XML字符串以及如何将XML文本转换回Java对象。该类使用XStream库进行操作,并提供了写XML到文件和从文件读取XML的功能。
摘要由CSDN通过智能技术生成

/**

* 输出xml和解析xml的工具类

*@ClassName:XmlUtil

*@author: chenyoulong Email: chen.youlong@payeco.com

*@date :2012-9-29 上午9:51:28

*@Description:TODO

*/

public class XmlUtil{

/**

* java 转换成xml

* @Title: toXml

* @Description: TODO

* @param obj 对象实例

* @return String xml字符串

*/

public static String toXml(Object obj){

XStream xstream=new XStream();

// XStream xstream=new XStream(new DomDriver()); //直接用jaxp dom来解释

// XStream xstream=new XStream(new DomDriver("utf-8")); //指定编码解析器,直接用jaxp dom来解释

如果没有这句,xml中的根元素会是;或者说:注解根本就没生效,所以的元素名就是类的属性

xstream.processAnnotations(obj.getClass()); //通过注解方式的,一定要有这句话

return xstream.toXML(obj);

}

/**

* 将传入xml文本转换成Java对象

* @Title: toBean

* @Description: TODO

* @param xmlStr

* @param cls xml对应的class类

* @return T xml对应的class类的实例对象

*

* 调用的方法实例:PersonBean person=XmlUtil.toBean(xmlStr,PersonBean.class);

*/

public static T toBean(String xmlStr,Class cls){

//注意:不是new Xstream(); 否则报错:java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory

XStream xstream=new XStream(new DomDriver());

xstream.processAnnotations(cls);

T obj=(T)xstream.fromXML(xmlStr);

return obj;

}

/**

* 写到xml文件中去

* @Title: writeXMLFile

* @Description: TODO

* @param obj 对象

* @param absPath 绝对路径

* @param fileName 文件名

* @return boolean

*/

public static boolean toXMLFile(Object obj,String absPath,String fileName ){

String strXml = toXml(obj);

String filePath = absPath + fileName;

File file = new File(filePath);

if(!file.exists()){

try {

file.createNewFile();

} catch (IOException e) {

log.error("创建{"+ filePath +"}文件失败!!!" + Strings.getStackTrace(e));

return false ;

}

}// end if

OutputStream ous = null ;

try {

ous = new FileOutputStream(file);

ous.write(strXml.getBytes());

ous.flush();

} catch (Exception e1) {

log.error("写{"+ filePath +"}文件失败!!!" + Strings.getStackTrace(e1));

return false;

}finally{

if(ous != null )

try {

ous.close();

} catch (IOException e) {

log.error("写{"+ filePath +"}文件关闭输出流异常!!!" + Strings.getStackTrace(e));

}

}

return true ;

}

/**

* 从xml文件读取报文

* @Title: toBeanFromFile

* @Description: TODO

* @param absPath 绝对路径

* @param fileName 文件名

* @param cls

* @throws Exception

* @return T

*/

public static T toBeanFromFile(String absPath,String fileName,Class cls) throws Exception{

String filePath = absPath +fileName;

InputStream ins = null ;

try {

ins = new FileInputStream(new File(filePath ));

} catch (Exception e) {

throw new Exception("读{"+ filePath +"}文件失败!",e);

}

String encode = useEncode(cls);

XStream xstream=new XStream(new DomDriver(encode));

xstream.processAnnotations(cls);

T obj =null;

try {

obj = (T)xstream.fromXML(ins);

} catch (Exception e) {

// TODO Auto-generated catch block

throw new Exception("解析{"+ filePath +"}文件失败!",e);

}

if(ins != null)

ins.close();

return obj;

}

}

http://www.cnblogs.com/XL-Liang/archive/2013/03/22/2974987.html

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值