来指定序列化的顺序。
第一步:定义java类时,使用
@XmlType(propOrder = { "id","name","age","book"})指定输出顺序。
- packagestep2;
- importjava.util.Set;
- importjavax.xml.bind.annotation.XmlElement;
- importjavax.xml.bind.annotation.XmlElementWrapper;
- importjavax.xml.bind.annotation.XmlRootElement;
- importjavax.xml.bind.annotation.XmlType;
- @XmlRootElement
- @XmlType(propOrder={"id","name","age","book"})
- publicclassCustomer{
- Stringname;
- intage;
- intid;
- Set<Book>book;
- @XmlElement(name="name")
- publicStringgetName(){
- returnname;
- }
- voidsetName(Stringname){
- this.name=name;
- @XmlElement(name="age")
- intgetAge(){
- returnage;
- voidsetAge(intage){
- this.age=age;
- @XmlElement(name="id")
- intgetId(){
- returnid;
- voidsetId(intid){
- this.id=id;
- @Override
- publicStringtoString(){
- return"Customer[id="+id+",name="+name+",age="+age+",book="+book+"]";
- @XmlElementWrapper(name="books")
- @XmlElement(name="book")
- publicSet<Book>getBook(){
- returnbook;
- voidsetBook(Set<Book>book){
- this.book=book;
- }
输出的xml:
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <customer>
- id>100</name>suoage>29booksbook>1>哈里波特price>100.0>2>苹果>50.0>
注:
1.对于@XmlElementWrapper标注的属性,不能出现在@XmlType的propOrder列表中。
原文链接:https://www.f2er.com/xml/294633.html