Class JAXBUtil

java.lang.Object
com.luna.common.xml.JAXBUtil

public class JAXBUtil extends Object
JAXB(Java Architecture for XML Binding),根据XML Schema产生Java对象,即实现xml和Bean互转。

相关介绍:

  • https://www.cnblogs.com/yanghaolie/p/11110991.html
  • https://my.oschina.net/u/4266515/blog/3330113
Since:
5.7.3
Author:
dazer
See Also:
  • Constructor Details

    • JAXBUtil

      public JAXBUtil()
  • Method Details

    • beanToXml

      public static String beanToXml(Object bean)
      JavaBean转换成xml

      bean上面用的常用注解

      Parameters:
      bean - Bean对象
      Returns:
      输出的XML字符串
      See Also:
      • @XmlRootElement(name = "school")
      • @XmlElement(name = "school_name", required = true)
      • @XmlElementWrapper(name="schools")
      • JAXB "有两个名为 "**" 的属性,类的两个属性具有相同名称 "**""解决方案
    • beanToXml

      public static String beanToXml(Object bean, Charset charset, boolean format)
      JavaBean转换成xml
      Parameters:
      bean - Bean对象
      charset - 编码 eg: utf-8
      format - 是否格式化输出eg: true
      Returns:
      输出的XML字符串
    • xmlToBean

      public static <T> T xmlToBean(String xml, Class<T> c)
      xml转换成JavaBean
      Type Parameters:
      T - Bean类型
      Parameters:
      xml - XML字符串
      c - Bean类型
      Returns:
      bean
    • xmlToBean

      public static <T> T xmlToBean(File file, Charset charset, Class<T> c)
      XML文件转Bean
      Type Parameters:
      T - Bean类型
      Parameters:
      file - 文件
      charset - 编码
      c - Bean类
      Returns:
      Bean
    • xmlToBean

      public static <T> T xmlToBean(Reader reader, Class<T> c)
      Reader中读取XML字符串,并转换为Bean
      Type Parameters:
      T - Bean类型
      Parameters:
      reader - Reader
      c - Bean类
      Returns:
      Bean