2024年Jaxb2 实现JavaBean与xml互转,Java开发者必看

文章讨论了互联网公司偏好的人才特点,包括对技术的热情、扎实的基础、主动性和团队协作能力。重点强调了高并发高可用技术的重要性,并提醒面试者要积极面对,关注公司文化而非仅薪资。同时提供了Java的JAXB技术示例和面试准备建议。
摘要由CSDN通过智能技术生成

总结

互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。

**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **

下面有部分截图希望能对大家有所帮助。

在这里插入图片描述

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • @XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。

三、示例

1、准备工作

Java代码   收藏代码

  1. package utils;

  2. import java.io.StringReader;

  3. import java.io.StringWriter;

  4. import javax.xml.bind.JAXBContext;

  5. import javax.xml.bind.Marshaller;

  6. import javax.xml.bind.Unmarshaller;

  7. /**

  8. * Jaxb2工具类

  9. * @author      zhuc

  10. * @create      2013-3-29 下午2:40:14

  11. */

  12. public class JaxbUtil {

  13. /**

  14. * JavaBean转换成xml

  15. * 默认编码UTF-8

  16. * @param obj

  17. * @param writer

  18. * @return

  19. */

  20. public static String convertToXml(Object obj) {

  21. return convertToXml(obj, “UTF-8”);

  22. }

  23. /**

  24. * JavaBean转换成xml

  25. * @param obj

  26. * @param encoding

  27. * @return

  28. */

  29. public static String convertToXml(Object obj, String encoding) {

  30. String result = null;

  31. try {

  32. JAXBContext context = JAXBContext.newInstance(obj.getClass());

  33. Marshaller marshaller = context.createMarshaller();

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

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

  36. StringWriter writer = new StringWriter();

  37. marshaller.marshal(obj, writer);

  38. result = writer.toString();

  39. } catch (Exception e) {

  40. e.printStackTrace();

  41. }

  42. return result;

  43. }

  44. /**

  45. * xml转换成JavaBean

  46. * @param xml

  47. * @param c

  48. * @return

  49. */

  50. @SuppressWarnings(“unchecked”)

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

  52. T t = null;

  53. try {

  54. JAXBContext context = JAXBContext.newInstance©;

  55. Unmarshaller unmarshaller = context.createUnmarshaller();

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

  57. } catch (Exception e) {

  58. e.printStackTrace();

  59. }

  60. return t;

  61. }

  62. }

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

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. }

  41. /* (non-Javadoc)

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

  43. */

  44. @Override

  45. public String toString() {

  46. return “Role [name=” + name + “, desc=” + desc + “]”;

  47. }

  48. }

Java代码   收藏代码

  1. package t2;

  2. import org.junit.Test;

  3. import utils.JaxbUtil;

  4. /**

  5. * @author      zhuc

  6. * @create      2013-3-29 下午2:52:00

  7. */

  8. public class JaxbTest2 {

  9. @Test

  10. public void showMarshaller() {

  11. Student student = new Student();

  12. student.setId(12);

面试资料整理汇总

成功从小公司跳槽进蚂蚁定级P7,只因刷了七遍这些面试真题

成功从小公司跳槽进蚂蚁定级P7,只因刷了七遍这些面试真题

这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。

面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了

在这里祝大家能够拿到心仪的offer!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  1. import org.junit.Test;

  2. import utils.JaxbUtil;

  3. /**

  4. * @author      zhuc

  5. * @create      2013-3-29 下午2:52:00

  6. */

  7. public class JaxbTest2 {

  8. @Test

  9. public void showMarshaller() {

  10. Student student = new Student();

  11. student.setId(12);

面试资料整理汇总

[外链图片转存中…(img-DRkxvbtj-1715000939562)]

[外链图片转存中…(img-hyKv00Mr-1715000939563)]

这些面试题是我朋友进阿里前狂刷七遍以上的面试资料,由于面试文档很多,内容更多,没有办法一一为大家展示出来,所以只好为大家节选出来了一部分供大家参考。

面试的本质不是考试,而是告诉面试官你会做什么,所以,这些面试资料中提到的技术也是要学会的,不然稍微改动一下你就凉凉了

在这里祝大家能够拿到心仪的offer!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 12
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值