使用JAXB来实现Java合xml之间的转换
【摘要】 使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了。
//javaBean-->xml @Test public void test1() { try { JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller marshaller = jaxbCon...
使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了。
//javaBean-->xml @Test public void test1() { try { JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Marshaller marshaller = jaxbContext.createMarshaller(); User user1 = new User("张三", "zhangsan", 123); marshaller.marshal(user1, System.out); } catch (JAXBException e) { e.printStackTrace(); } } //xml-->javaBean @Test public void test2() { try { String xml = "<user><money>123</money>" + "<password>zhangsan</password><username>张三</username>" + "</user>"; JAXBContext jaxbContext = JAXBContext.newInstance(User.class); Unmarshaller um = jaxbContext.createUnmarshaller(); User user = (User) um.unmarshal(new StringReader(xml)); System.out.println(user.getUsername() + "," + user.getMoney()); } catch (JAXBException e) { e.printStackTrace(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。
原文链接:wangsong.blog.csdn.net/article/details/45584087
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)