XmlUtil工具类(toxml()和toBean())

  1. /**

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

  3. *@ClassName:XmlUtil

  4. *@author: chenyoulong Email: [email protected]

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

  6. *@Description:TODO

  7. */

  8. public class XmlUtil{

  9. /**

  10. * java 转换成xml

  11. * @Title: toXml

  12. * @Description: TODO

  13. * @param obj 对象实例

  14. * @return String xml字符串

  15. */

  16. public static String toXml(Object obj){

  17. XStream xstream=new XStream();

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

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

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

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

  23. return xstream.toXML(obj);

  24. }

  25.  
  26. /**

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

  28. * @Title: toBean

  29. * @Description: TODO

  30. * @param xmlStr

  31. * @param cls xml对应的class类

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

  33. *

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

  35. */

  36. public static <T> T toBean(String xmlStr,Class<T> cls){

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

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

  39. xstream.processAnnotations(cls);

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

  41. return obj;

  42. }

  43.  
  44. /**

  45. * 写到xml文件中去

  46. * @Title: writeXMLFile

  47. * @Description: TODO

  48. * @param obj 对象

  49. * @param absPath 绝对路径

  50. * @param fileName 文件名

  51. * @return boolean

  52. */

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

  55. String strXml = toXml(obj);

  56. String filePath = absPath + fileName;

  57. File file = new File(filePath);

  58. if(!file.exists()){

  59. try {

  60. file.createNewFile();

  61. } catch (IOException e) {

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

  63. return false ;

  64. }

  65. }// end if

  66. OutputStream ous = null ;

  67. try {

  68. ous = new FileOutputStream(file);

  69. ous.write(strXml.getBytes());

  70. ous.flush();

  71. } catch (Exception e1) {

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

  73. return false;

  74. }finally{

  75. if(ous != null )

  76. try {

  77. ous.close();

  78. } catch (IOException e) {

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

  80. }

  81. }

  82. return true ;

  83. }

  84.  
  85. /**

  86. * 从xml文件读取报文

  87. * @Title: toBeanFromFile

  88. * @Description: TODO

  89. * @param absPath 绝对路径

  90. * @param fileName 文件名

  91. * @param cls

  92. * @throws Exception

  93. * @return T

  94. */

  95. public static <T> T toBeanFromFile(String absPath, String fileName,Class<T> cls) throws Exception{

  96. String filePath = absPath +fileName;

  97. InputStream ins = null ;

  98. try {

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

  100. } catch (Exception e) {

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

  102. }

  103.  
  104. String encode = useEncode(cls);

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

  106. xstream.processAnnotations(cls);

  107. T obj =null;

  108. try {

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

  110. } catch (Exception e) {

  111. // TODO Auto-generated catch block

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

  113. }

  114. if(ins != null)

  115. ins.close();

  116. return obj;

  117. }

  118.  
  119. }

猜你喜欢

转载自blog.csdn.net/yangzhengjianglove/article/details/81237209