JAXB annotation note and its use

Due to a recent study REST ,, jersey of the demo uses JAXB, on the way to understand a bit.
JAXB concept is not a class of records.
Their usual annotation has the following:


@XmlType
@XmlElement
@XmlRootElement
@XmlAttribute
@XmlAccessorType
@XmlAccessorOrder
@XmlTransient
@XmlJavaTypeAdapter


among them


@XmlType
@XmlRootElement
@XmlAccessorType
@XmlAccessorOrder
Used over class.
For a class, may be used @XmlAccessorType (XmlAccessType.FIELD) to label the entire class member variables are accessed when converted to xml. If each of the object need to be set individually, i.e. to set its properties, or xml element, it can by @ XmlElement, @ XmlAttribute individually denoted.
The following is a demo JAXB of

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class CC {
String name;
int age;
int id;
List<Book> book = new ArrayList<Book>();

@XmlElementWrapper(name="books")
@XmlElement
public List<Book> getBook() {
return book;
}

public void setBook(List<Book> book) {
this.book = book;
}

@XmlElement
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@XmlElement
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@XmlAttribute
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "Customer [id=" + id + ",name=" + name + ",age=" + age + "]";
}


package com.tectest;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {

private String id;
private String name;
private int price;

@XmlAttribute
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@XmlElement
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@XmlElement
public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}




import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

//Marshaller
public class Object2XmlDemo2 {
public static void main(String[] args) {

CC c = new CC();
c.setId(200);
c.setName("suo");
c.setAge(29);


Book book = new Book();
book.setId("1");
book.setName("哈里波特");
book.setPrice(100);

Book book2 = new Book();
book2.setId("2");
book2.setName("苹果");
book2.setPrice(50);

List<Book> bookSet = new ArrayList<Book>();
bookSet.add(book);
bookSet.add(book2);

c.setBook(bookSet);
try {
File file = new File("D:\\file3.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(CC.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
jaxbMarshaller.marshal(c, file);
jaxbMarshaller.marshal(c, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}

Results of the:

<cc id="200">
<age>29</age>
<books>
<book id="1">
<name>哈里波特</name>
<price>100</price>
</book>
<book id="2">
<name>苹果</name>
<price>50</price>
</book>
</books>
<name>xxx</name>
</cc>
去掉CC其中的annotation@XmlElementWrapper(name="books")后,执行结果如下。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cc>
<name>xxx</name>
<age>29</age>
<id>200</id>
<book id="1">
<name>哈里波特</name>
<price>100</price>
</book>
<book id="2">
<name>苹果</name>
<price>50</price>
</book>
</cc>
而如果使用@XmlAttribute设置CC的id属性后,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>29</age>
<book id="1">
<name>哈里波特</name>
<price>100</price>
</book>
<book id="2">
<name>苹果</name>
<price>50</price>
</book>
<name>xxx</name>
</customer>



Each annotation is described:
[quote]

@XmlType

  @XmlType annotation used in class, which is often associated with @ XmlRootElement, used with @ XmlAccessorType. It has three attributes: name, propOrder, namespace, only the first two attributes often used. Such as:

@XmlType (name = "basicStruct", propOrder = {
"intValue",
"stringArray",
"the stringValue"
)

when using the @XmlType propOrder properties, list all the properties must be JavaBean object, otherwise an error.

  @ The XmlElement 2.

  @XmlElement the java attribute xml node mapping object in use @XmlElement, java object properties may change the name displayed in the name attribute xml through. Such as:
  @XmlElement (name = "the Address")  
  Private String yourAddress;
  3. @ XmlRootElement

  the @XmlRootElement annotation for class level, with elements corresponding to the xml, and is often used in conjunction with @XmlType @XmlAccessorType. Such as:
  @XmlType
  @XmlAccessorType (XmlAccessType.
  The @XmlRootElement
  public class {} the Address
  4. @ XmlAttribute
  @XmlAttribute java object for mapping the attributes of the attribute xml, and aliased xml attribute generated by the name attribute. Such as:
  @XmlAttribute (name = "Country")
  Private State String;
  5. The @ XmlAccessorType

  @XmlAccessorType access manner java object attribute xml file is generated by the java object specifies. Often with @ XmlRootElement, used with @ XmlType. Its property values are four enumeration values XmlAccessType, namely:

  XmlAccessType.FIELD: all member variables java object

  XmlAccessType.PROPERTY:java objects in all member variables accessed through getter / setter way

  XmlAccessType.PUBLIC_MEMBER: java objects All public member variables and access to member variables accessed through getter / setter way

  XmlAccessType.NONE: all attributes java objects are not mapped to the elements of xml

  Note: The default access level @XmlAccessorType is XmlAccessType.PUBLIC_MEMBER, therefore, if the private member variables java object permissions set public getter / setter methods, do not use @XmlElement and @XmlAttribute comment on private variables, or by java object will be reported in the same property twice in the presence of an error is generated java class xml. Similarly, if @XmlAccessorType access to XmlAccessType.NONE, if you use @XmlElement or @XmlAttribute comment on the member variable java, which still can be mapped to member variables xml file.
  @ XmlAccessorOrder 6. The

  @XmlAccessorOrder xml element for generating sort java object. It has two attribute values:

  AccessorOrder.ALPHABETICAL: xml element for generating a sequence of alphabetical letter

  XmlAccessOrder.UNDEFINED: no sorting
  7. The @ XmlTransient

  @XmlTransient for indicating when a java object mapping xml, this property is ignored. In other words, this element does not appear in the xml file generated.
  @ With XmlJavaTypeAdapter 8. The

  @XmlJavaTypeAdapter used in the conversion of more complex objects, such as the type or format date map. With this comment, you need to write your own adapter class inherits XmlAdapter abstract class, and inside methods.

  @XmlJavaTypeAdapter (value = xxx.class), value of its own class definition adapter

  XmlAdapter如下:

public abstract class XmlAdapter<ValueType,BoundType> {
// Do-nothing constructor for the derived classes.
protected XmlAdapter() {}
// Convert a value type to a bound type.
public abstract BoundType unmarshal(ValueType v);
// Convert a bound type to a value type.
public abstract ValueType marshal(BoundType v);
}
[/quote]
以上说明转自[url]http://www.cnblogs.com/fragranting/archive/2012/03/25/xml--jaxb.html[/url]
Published 56 original articles · won praise 0 · Views 7781

Guess you like

Origin blog.csdn.net/chainhou/article/details/84442836