bean转化为xml 与 xml转化为bean

前端之家收集整理的这篇文章主要介绍了bean转化为xml 与 xml转化为bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、bean to xml:

public static void main(String[] args) {

List<JydInfo> list = new ArrayList<JydInfo>();
String auth_code = "hunan_auth_code";

JydInfo clws1 = new JydInfo("100101","黑猪","100","2000","12.5","11111111111111111111,222222222222,33333333333333",
"20140811434240101011","2014-04-13","2014-08-11","散户","1舍","潘文",
"2014年3月29号出生;4月23号打猪瘟疫苗;4月29号断奶,打蓝耳,伪狂犬,口蹄疫疫苗;使用尿素作牛羊饲料的添加剂,取得良好效果;",
"434240101","红星养殖场","034022031","胡泽","湘A88356");

JydInfo clws2 = new JydInfo("100102","白猪","1000","20000","13.5","33333333333,111111111111","2014-05-13","2014-08-21","2舍",
"2014年4月29号出生;5月23号打猪瘟疫苗;5月29号断奶,打蓝耳,伪狂犬,口蹄疫疫苗;使用尿素作牛羊饲料的添加剂,取得良好效果;","章泽","湘A11111");
list.add(clws1);
list.add(clws2);
JydInfos jydinfos = new JydInfos(list);
Req req = new Req(auth_code,jydinfos);//这个类包含两个成员变量:list类型和一个String类型
try {
JAXBContext context = JAXBContext.newInstance(Req.class);
Marshaller marshaller = context.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(req,sw);
//转换后的xml
System.out.println(sw.toString());
} catch (JAXBException e) {
e.printStackTrace();
}

}


2、bean to xml:

System.out.println("---传送过来的xml----"+obj[0]); try { JAXBContext context = JAXBContext.newInstance(Res.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Res u = (Res)unmarshaller.unmarshal(new StringReader(obj[0])); //返回的东西 System.out.println("状态:"+u.getData().getZt()+","+u.getResult().getMessage()+","+u.getResult().getCode()); } catch (JAXBException e) { e.printStackTrace(); } 原文链接:https://www.f2er.com/xml/297350.html

猜你在找的XML相关文章