JAXB简析(xml与object转换)

前端之家收集整理的这篇文章主要介绍了JAXB简析(xml与object转换)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简单介绍

JAXB是一项可以实现java对象与xml文件进行转换的技术。

涉及的几个类

(1)JAXBContext 理解为一个管理者
(2)Marshaller 将java对象写到文件
(3)Unmarshaller 将xml解析为java对象

实例

实体类:

  1. import java.io.Serializable;
  2. import java.util.Locale;
  3.  
  4. import javax.xml.bind.annotation.XmlAccessType;
  5. import javax.xml.bind.annotation.XmlAccessorType;
  6. import javax.xml.bind.annotation.XmlElement;
  7. import javax.xml.bind.annotation.XmlRootElement;
  8.  
  9. @XmlRootElement(name = "ClientConfig")
  10. @XmlAccessorType(XmlAccessType.FIELD)
  11. public class ClientConfig implements Serializable{
  12. private static final long serialVersionUID = 20131015L;
  13.  
  14. public static final String CONFIG_NAME = "ClientConfig";
  15.  
  16. @XmlElement(required = true)
  17. private String code;
  18.  
  19. @XmlElement(required = true)
  20. private String language;
  21.  
  22. @XmlElement(required = true)
  23. private String country;
  24.  
  25. @XmlElement(required = true)
  26. private boolean developMode;
  27.  
  28. @XmlElement(required = false,defaultValue="false")
  29. private boolean testMode;
  30.  
  31. @XmlElement(required = true)
  32. private String wsURL;
  33.  
  34.  
  35. public String getCode() {
  36. return code;
  37. }
  38.  
  39. public String getLanguage() {
  40. return language;
  41. }
  42.  
  43. public String getCountry() {
  44. return country;
  45. }
  46.  
  47. public boolean isDevelopMode() {
  48. return developMode;
  49. }
  50.  
  51. public String getWsURL() {
  52. return wsURL;
  53. }
  54.  
  55. public boolean isTestMode() {
  56. return testMode;
  57. }
  58. }

将对象写入到xml文件

  1. import java.io.File;
  2.  
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6.  
  7. public class JAXB {
  8. public static void main(String[] args) throws JAXBException {
  9. JAXBContext jaxbContext = JAXBContext.newInstance(ClientConfig.class);
  10. Marshaller marshaller = jaxbContext.createMarshaller();
  11. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);//格式化输出的xml
  12. marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");// 设置输出编码,默认为UTF-8
  13. ClientConfig c = new ClientConfig();
  14. c.setXxx("er");//设置属性
  15. marshaller.marshal(c,new File("path"));//path为文件路径
  16. }
  17. }

将xml文件解析为java对象

  1. Reader reader = new FileReader(configFile);
  2. JAXBContext context = JAXBContext.newInstance(ClientConfig.class);
  3. Unmarshaller unmarshaller = context.createUnmarshaller();
  4. clientConfig = (ClientConfig)unmarshaller.unmarshal(reader);

小记

(1)@XmlRootElement,将Java类或枚举类型映射到XML元素;

(2)@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素;

(3)@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性

注意事项:

(1)对于要序列化(marshal)为XML的Java类,绝不能把成员变量声明为public,否则运行将抛出异常

  com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException

(2)注解不能直接放在成员变量上,可以放在成员变量的getter或setter方法上,任选其一,否则也会抛出IllegalAnnotationsException异常

小结

本文为自己工作中遇到然后系统看了一下,其中也参考了此文章JavaEE学习之JAXB

猜你在找的XML相关文章