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

bean转化为xml 与 xml转化为bean

发布时间:2020-12-16 06:30:47 所属栏目:百科 来源:网络整理
导读:1、bean to xml: public static void main(String[] args) { ListJydInfo list = new ArrayListJydInfo(); String auth_code = "hunan_auth_code"; JydInfo clws1 = new JydInfo("100101","黑猪","100","2000","12.5","22222222222222222222,222222222222,33

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","22222222222222222222,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,222222222211","2014-05-13","2014-08-21","2舍",
"2014年4月29号出生;5月23号打猪瘟疫苗;5月29号断奶,打蓝耳,伪狂犬,口蹄疫疫苗;使用尿素作牛羊饲料的添加剂,取得良好效果;","章泽","湘A22222");
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(); }

(编辑:李大同)

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

    推荐文章
      热点阅读