Jaxb2 实现JavaBean与xml互转,2024阿里、网易、京东等大厂最新Android面试题

  1. Marshaller marshaller = context.createMarshaller();

  2. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

  3. marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

  4. StringWriter writer = new StringWriter();

  5. marshaller.marshal(obj, writer);

  6. result = writer.toString();

  7. } catch (Exception e) {

  8. e.printStackTrace();

  9. }

  10. return result;

  11. }

  12. /**

  13. * xml转换成JavaBean

  14. * @param xml

  15. * @param c

  16. * @return

  17. */

  18. @SuppressWarnings(“unchecked”)

  19. public static  T converyToJavaBean(String xml, Class c) {

  20. T t = null;

  21. try {

  22. JAXBContext context = JAXBContext.newInstance©;

  23. Unmarshaller unmarshaller = context.createUnmarshaller();

  24. t = (T) unmarshaller.unmarshal(new StringReader(xml));

  25. } catch (Exception e) {

  26. e.printStackTrace();

  27. }

  28. return t;

  29. }

  30. }

非常简单易懂,需要注意的是

Java代码   收藏代码

  1. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

  2. marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

Marshaller.JAXB_FORMATTED_OUTPUT 决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml)

Marshaller.JAXB_ENCODING xml的编码方式

另外,Marshaller 还有其他Property可以设置,可以去查阅api。

2、最简单转换

Java代码   收藏代码

  1. package t1;

  2. import java.util.Date;

  3. import javax.xml.bind.annotation.XmlAccessType;

  4. import javax.xml.bind.annotation.XmlAccessorType;

  5. import javax.xml.bind.annotation.XmlAttribute;

  6. import javax.xml.bind.annotation.XmlElement;

  7. import javax.xml.bind.annotation.XmlRootElement;

  8. import javax.xml.bind.annotation.XmlType;

  9. /**

  10. * @author      zhuc

  11. * @create      2013-3-29 下午2:49:48

  12. */

  13. @XmlAccessorType(XmlAccessType.FIELD)

  14. @XmlRootElement

  15. @XmlType(name = “book”, propOrder = { “author”, “calendar”, “price”, “id” })

  16. public class Book {

  17. @XmlElement(required = true)

  18. private String author;

  19. @XmlElement(name = “price_1”, required = true)

  20. private float price;

  21. @XmlElement

  22. private Date calendar;

  23. @XmlAttribute

  24. private Integer id;

  25. /**

  26. * @return the author

  27. */

  28. public String getAuthor() {

  29. return author;

  30. }

  31. /**

  32. * @return the price

  33. */

  34. public float getPrice() {

  35. return price;

  36. }

  37. /**

  38. * @return the calendar

  39. */

  40. public Date getCalendar() {

  41. return calendar;

  42. }

  43. /**

  44. * @return the id

  45. */

  46. public Integer getId() {

  47. return id;

  48. }

  49. /**

  50. * @param author the author to set

  51. */

  52. public void setAuthor(String author) {

  53. this.author = author;

  54. }

  55. /**

  56. * @param price the price to set

  57. */

  58. public void setPrice(float price) {

  59. this.price = price;

  60. }

  61. /**

  62. * @param calendar the calendar to set

  63. */

  64. public void setCalendar(Date calendar) {

  65. this.calendar = calendar;

  66. }

  67. /**

  68. * @param id the id to set

  69. */

  70. public void setId(Integer id) {

  71. this.id = id;

  72. }

  73. /* (non-Javadoc)

  74. * @see java.lang.Object#toString()

  75. */

  76. @Override

  77. public String toString() {

  78. return “Book [author=” + author + “, price=” + price + “, calendar=” + calendar + “, id=” + id + “]”;

  79. }

  80. }

Java代码   收藏代码

  1. package t1;

  2. import java.util.Date;

  3. import javax.xml.bind.JAXBException;

  4. import org.junit.Test;

  5. import utils.JaxbUtil;

  6. /**

  7. * @author      zhuc

  8. * @create      2013-3-29 下午2:50:00

  9. */

  10. public class JaxbTest1 {

  11. /**

  12. * @throws JAXBException

  13. */

  14. @Test

  15. public void showMarshaller()  {

  16. Book book = new Book();

  17. book.setId(100);

  18. book.setAuthor(“James”);

  19. book.setCalendar(new Date());

  20. book.setPrice(23.45f);   //默认是0.0

  21. String str = JaxbUtil.convertToXml(book);

  22. System.out.println(str);

  23. }

  24. /**

  25. * @throws JAXBException

  26. */

  27. @Test

  28. public void showUnMarshaller() {

  29. String str = “<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>” +

  30. “<book id=\“100\”>” +

  31. "    James" +

  32. "   2013-03-29T09:25:56.004+08:00" +

  33. "  <price_1>23.45</price_1>" +

  34. “”;

  35. Book book = JaxbUtil.converyToJavaBean(str, Book.class);

  36. System.out.println(book);

  37. }

  38. }

输出结果分别为:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

< book id=“100”>

James

2013-03-29T14:50:58.974+08:00

<price_1>23.45</price_1>

< /book>

Book [author=James, price=23.45, calendar=Fri Mar 29 09:25:56 CST 2013, id=100]

3、类中包含复杂对象的转换

Java代码   收藏代码

  1. package t2;

  2. import javax.xml.bind.annotation.XmlAccessType;

  3. import javax.xml.bind.annotation.XmlAccessorType;

  4. import javax.xml.bind.annotation.XmlAttribute;

  5. import javax.xml.bind.annotation.XmlElement;

  6. import javax.xml.bind.annotation.XmlRootElement;

  7. import javax.xml.bind.annotation.XmlType;

  8. /**

  9. * @author      zhuc

  10. * @create      2013-3-29 下午2:51:44

  11. */

  12. @XmlAccessorType(XmlAccessType.FIELD)

  13. @XmlRootElement(name = “student”)

  14. @XmlType(propOrder = {})

  15. public class Student {

  16. @XmlAttribute

  17. private Integer id;

  18. @XmlElement

  19. private String name;

  20. @XmlElement(name = “role”)

  21. private Role role;

  22. /**

  23. * @return the id

  24. */

  25. public Integer getId() {

  26. return id;

  27. }

  28. /**

  29. * @return the name

  30. */

  31. public String getName() {

  32. return name;

  33. }

  34. /**

  35. * @return the role

  36. */

  37. public Role getRole() {

  38. return role;

  39. }

  40. /**

  41. * @param id the id to set

  42. */

  43. public void setId(Integer id) {

  44. this.id = id;

  45. }

  46. /**

  47. * @param name the name to set

  48. */

  49. public void setName(String name) {

  50. this.name = name;

  51. }

  52. /**

  53. * @param role the role to set

  54. */

  55. public void setRole(Role role) {

  56. this.role = role;

  57. }

  58. /* (non-Javadoc)

  59. * @see java.lang.Object#toString()

  60. */

  61. @Override

  62. public String toString() {

  63. return “Student [id=” + id + “, name=” + name + “, role=” + role + “]”;

  64. }

  65. }

Java代码   收藏代码

  1. package t2;

  2. import javax.xml.bind.annotation.XmlAccessType;

  3. import javax.xml.bind.annotation.XmlAccessorType;

  4. import javax.xml.bind.annotation.XmlElement;

  5. import javax.xml.bind.annotation.XmlType;

  6. /**

  7. * @author      zhuc

  8. * @create      2013-3-29 下午2:51:52

  9. */

  10. @XmlAccessorType(XmlAccessType.FIELD)

  11. @XmlType(propOrder = { “name”, “desc” })

  12. public class Role {

  13. @XmlElement

  14. private String name;

  15. @XmlElement

  16. private String desc;

  17. /**

  18. * @return the name

  19. */

  20. public String getName() {

  21. return name;

  22. }

  23. /**

  24. * @return the desc

  25. */

  26. public String getDesc() {

  27. return desc;

  28. }

  29. /**

  30. * @param name the name to set

  31. */

  32. public void setName(String name) {

  33. this.name = name;

  34. }

  35. /**

  36. * @param desc the desc to set

  37. */

  38. public void setDesc(String desc) {

  39. this.desc = desc;

  40. }

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术

高级UI,自定义View

UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。

不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!

9147)]
[外链图片转存中…(img-SzO2kCrL-1711777349148)]
[外链图片转存中…(img-vB5M0UMr-1711777349148)]
[外链图片转存中…(img-jlcWuqst-1711777349148)]
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-L4riGy7c-1711777349149)]

最后

下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术

[外链图片转存中…(img-22R2EHDe-1711777349149)]

高级UI,自定义View

UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。

不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!

[外链图片转存中…(img-Q9Ja39nu-1711777349149)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值