首页 > 其他分享 >JAXB中各种常见注解小结

JAXB中各种常见注解小结

时间:2022-12-02 12:05:41浏览次数:49  
标签:jdoe JAXB List public janed 注解 emailAddresses 小结 example


在JAXB中(用于JAVA对象和xml之间的转换),经常出现各类的 @XmlElement这样的标记,
下面就来以一个例子小结下,加深学习:


[code="java"]


import java.util.*;
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    private List<String> emailAddresses;
     
    public Customer() {
        emailAddresses = new ArrayList<String>();
    }
 
    public List<String> getEmailAddresses() {
        return emailAddresses;
    }
 
    public void setEmailAddresses(List<String> emailAddresses) {
        this.emailAddresses = emailAddresses;
    }
 
}


[/code]


 这个是一个典型的POJO了,其中引用了对象emailAddress,是一个List,那么下面演示各类用法:
  先看默认的调用代码如下:
[code="java"]
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
 
        Customer customer = new Customer();
        customer.getEmailAddresses().add("[email protected]");
        customer.getEmailAddresses().add("[email protected]");
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
}
[/code]


  默认的把这个对象输出为一般的XML:
<customer>
    <emailAddresses>[email protected]</emailAddresses>
    <emailAddresses>[email protected]</emailAddresses>
</customer>


 下面逐一看每个注解的用法
1) @XmlElement
   
 @XmlElement(name="email-address")
    private List<String> emailAddresses;
 加上这个注解的话,则按自定义的xml标签名去输出某个属性,如下:
<customer>
    <email-address>[email protected]</email-address>
    <email-address>[email protected]</email-address>
</customer>


2) @XmlElementWrapper
  这个注解等于在最外面再包一层了,
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlElementWrapper(name="email-addresses")
    @XmlElement(name="email-address")
    private List<String> emailAddresses;
 
}
  输出:
<customer>
    <email-addresses>
        <email-address>[email protected]</email-address>
        <email-address>[email protected]</email-address>
    </email-addresses>
</customer>


 
3) @XmlList
  这个等于是在同一行中,把list中的输出,以空格形式分隔开来,
[code="java"]
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
 
    @XmlList
    private List<String> emailAddresses;
 
}
[/code]


  输出:
  <customer>
    <emailAddresses>[email protected] [email protected]</emailAddresses>
</customer>


4)
@XmlList和 @XmlAttribute混合使用
      @XmlList
    @XmlAttribute
    private List<String> emailAddresses;
 
输出:
   <customer
    emailAddresses="[email protected] [email protected]"/>


看到没?就是同一行中逗号输出,并作为customer的一个属性




5)
  @XmlList 和 @XmlValue混用
    
    @XmlList
    @XmlValue
    private List<String> emailAddresses;
  


就是把emailAddress的list的值,作为<customer>的value  输出,结果如下:
 <customer>[email protected] [email protected]</customer>
  

标签:jdoe,JAXB,List,public,janed,注解,emailAddresses,小结,example
From: https://blog.51cto.com/u_14230175/5906863

相关文章

  • jackson快速小结
    1对象转换为jsonObjectMappermapper=newObjectMapper();Staffobj=newStaff();mapper.writeValue(newFile("c:\\file.json"),obj);String......
  • android中的menu和子menu小结
    menu.xml<?xmlversion="1.0"encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/Menu1"android:tit......
  • 挂载文件系统选项nodiratime、noatime等集合小结
    Linux系统文件有三个主要的时间属性,分别是ctime(changetime),atime(accesstime),mtime(modifytime)。这三个时间很容易混淆,准备深入了解Linux的......
  • jquery中$.each小结
    在jquery中,$each的用法比较常见,下面小结下1)基本用法//ARRAYSvararr=['one','two','three','four','five'];$.each(arr,funct......
  • pidstat监控工具小结
    pidstat 是著名的采集软件systat的组件之一。安装用yuminstall  就可以了。1)pidstat  结果分析  %usr-当在用户层执行(应用程序)时这个任务的cpu使用率,和ni......
  • FTP两种传输模式小结
    FTP是有两种传输的模式的,主动模式和被动模式,之前一直没怎么去搞明白之,现在找了下资料,重新整理了下: 一个完整的FTP文件传输需要建立两种类型的连......
  • ATLAS拖拉之简单小结
    有了atlas的话,做一些随意拖拉的效果就十分容易了。在vs.net2005下,装了atlas的话,有很多控件可以实现之,下面小结之1、使用<atlas:DragOverlayProperties>控件,比如 <atlas:S......
  • 获取参数,把方法上的参数绑定到注解的变量中
    packagecom.geekmore.modules.device.aop;importjava.lang.reflect.Method;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.reflect.MethodSignature;/***......
  • 快速小结:CSS3盒模型
    ......
  • javascript中generator快速小结
    1基本例子  function*generatorFunc(){console.log("任务一");yield1;console.log("任务二");yield*generatorSubFunc();console.log("任务三");return......