JAXB 实现java对象与xml之间互相转换

来源:互联网 发布:java对象转json 编辑:程序博客网 时间:2024/04/30 18:34

利用Marshaller和unMarshaller可在java的object对象和xml之间实现转换

首先创建一个简单的Boy对象

[java] view plaincopyprint?
  1. @XmlRootElement(name="Root")  
  2. @XmlAccessorType(XmlAccessType.PROPERTY)  
  3. public class Boy {  
  4.       
  5.     private String name = "aa";  
  6.   
  7.     public String getName() {  
  8.         return name;  
  9.     }  
  10.   
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.       
  15. }  

其中@XmlRootElement(name="Root")表明xml的根元素,(name="Root")这个是重新定义xml文件的跟元素,如果没有此name定义,则xml根元素默认跟对象名一致

然后通过Marshaller类实现将对象转换为xml,同时也可利用Unmarshaller类进行xml和类之间的转换

[java] view plaincopyprint?
  1. public class JAXBTest {  
  2.   
  3.     public static void main(String[] args) throws JAXBException {  
  4.         JAXBContext context = JAXBContext.newInstance(Boy.class);  
  5.           
  6.         Marshaller marshaller = context.createMarshaller();  
  7.         Unmarshaller unMarshaller = context.createUnmarshaller();  
  8.           
  9.         System.out.println("----------marshaller--------------");  
  10.         Boy boy = new Boy();  
  11.         marshaller.marshal(boy, System.out);  
  12.           
  13.         System.out.println("\n----------unMarshaller--------------");  
  14.         //将xml转换为对应的java对象   
  15.         String xml = "<Root><name>aa</name></Root>";//此处标签名称须和boy对象的属性一致  
  16.         Boy b = (Boy) unMarshaller.unmarshal(new StringReader(xml));  
  17.         System.out.println(b.getName());  
  18.     }  
  19.       
  20. }  
最后转换打印结果

----------marshaller--------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root><name>aa</name></Root>
----------unMarshaller--------------
aa

上面介绍的对象boy中只包含简单属性,如果boy中还包含其他对象该做如何处理呢?

[java] view plaincopyprint?
  1. @XmlRootElement(name="Root")  
  2. @XmlAccessorType(XmlAccessType.PROPERTY)  
  3. public class Boy {  
  4.       
  5.     private String name = "aa";  
  6.     private Address address;  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.       
  16.     @XmlJavaTypeAdapter(AddressAdapter.class)  
  17.     public Address getAddress() {  
  18.         return address;  
  19.     }  
  20.       
  21.     public void setAddress(Address address) {  
  22.         this.address = address;  
  23.     }  
  24. }  
[java] view plaincopyprint?
  1. public class Address {  
  2.   
  3.     private String address;  
  4.       
  5.     public void setAddress(String address) {  
  6.         this.address = address;  
  7.     }  
  8.   
  9.     public String getAddress() {  
  10.         return address;  
  11.     }  
  12.   
  13. }  

此时的boy对象中包含了Address对象,所以此时boy对象中的getAddress()须设置adapter,

创建AddressAdapter类,继承XmlAdapter<ValueType,BoundType>

[java] view plaincopyprint?
  1. public class AddressAdapter extends XmlAdapter<String, Address> {  
  2.   
  3.     @Override  
  4.     public Address unmarshal(String v) throws Exception {  
  5.         AddressImpl address = new AddressImpl();  
  6.         address.setAddress(v);  
  7.         return address;  
  8.     }  
  9.   
  10.     @Override  
  11.     public String marshal(Address v) throws Exception {  
  12.         return v.getAddress();  
  13.     }  
  14.   
  15. }  


此时即可进行java对象转换为xml

[java] view plaincopyprint?
  1. public class JAXBTest {  
  2.   
  3.     public static void main(String[] args) throws JAXBException {  
  4.         JAXBContext context = JAXBContext.newInstance(Boy.class);  
  5.           
  6.         Marshaller marshaller = context.createMarshaller();  
  7.           
  8.         System.out.println("----------marshaller--------------");  
  9.         Boy boy = new Boy();  
  10.         AddressImpl address = new AddressImpl();  
  11.         address.setAddress("BeiJing");  
  12.         boy.setAddress(address);  
  13.         marshaller.marshal(boy, System.out);  
  14.           
  15.     }  
  16.       
  17. }  

最后打印结果为:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Root><address>BeiJing</address><name>aa</name></Root>

原创粉丝点击