Java code
- @Retention (Value=runtime)
- @Target (Value=type)
- Public @interface Xmlrootelement
- @Inherited
- @Retention (Value=runtime)
- @Target (Value={package,type})
- Public @interface Xmlaccessortype
Xmlrootelement: Maps A class or enumeration type to an XML element. A note in JAXB that is used to generate XML content from a Java class.
When you annotate a top-level class or enumeration type with a @XmlRootElement comment, the type value is represented as an XML element in the XML document.
JAXB Annotation
- @XmlRootElement The root element of the //xml file
- @XmlElement
- @XmlAccessorType /Control whether the word is by default The segment or Javabean property is serialized.
- @XmlTransient
- @XmlJavaTypeAdaptor: Reference using JAXB 2.0 ' s xmljavatypeadapter
Xmlaccessortype
Default rule:
By default, if @XmlAccessorType are not present in the package, the following package-level comments are assumed to be used.
@XmlAccessorType (Xmlaccesstype.public_member)
By default, if @XmlAccessorType is not present in the class and no superclass is used @XmlAccessorType Comment, the following default comment is assumed to be used in the class:
@XmlAccessorType (Xmlaccesstype.public_member)
Possible values:
Each non-static, non-transient field in the Field:jaxb binding class is automatically bound to XML unless annotated by xmltransient.
None: None of the fields or properties can be bound to XML unless they are specifically annotated with some JAXB annotations.
Each fetch method/set method pair in the PROPERTY:JAXB binding class will be automatically bound to XML unless annotated by xmltransient.
Public_member: Each public get method/set method pair and each public field will be automatically bound to XML unless annotated by xmltransient.
Application reference: http://blog.sina.com.cn/s/blog_4051f5dc0100ju0a.html
Java code
- Package JAXB;
- Import Javax.xml.bind.annotation.XmlAccessorType;
- Import javax.xml.bind.annotation.XmlRootElement;
- Import Javax.xml.bind.annotation.XmlAccessType;
- @XmlRootElement //Must be marked with this element
- @XmlAccessorType (Xmlaccesstype.field)
- Public class Boy {
- String name = "CY";
- }
- Package JAXB;
- Import Java.io.StringReader;
- Import Javax.xml.bind.JAXBContext;
- Import javax.xml.bind.JAXBException;
- Import Javax.xml.bind.Marshaller;
- Import Javax.xml.bind.Unmarshaller;
- Public class Jaxbtest {
- public static void Main (string[] args) throws jaxbexception {
- Jaxbcontext context = Jaxbcontext.newinstance (boy. Class);
- Marshaller Marshaller = Context.createmarshaller ();
- Unmarshaller Unmarshaller = Context.createunmarshaller ();
- Boy boy = new boy ();
- Marshaller.marshal (Boy, System.out);
- System.out.println ();
- String xml = "<boy><name>David</name></boy>";
- Boy Boy2 = (boy) Unmarshaller.unmarshal (new StringReader (XML));
- System.out.println (Boy2.name);
- }
- }
execution Result:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><boy><name>cy</name></boy>
David
(
Change one:
Modify @xmlaccessortype (Xmlaccesstype.field) @XmlAccessorType ( Xmlaccesstype.property)
means
So the result of the run is:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy/>
CY
means that when Java object is converted to XML, name is not a property (because there is no get Set method), so name does not change to a label.
Change two:
Add a Get Set method to the Name property, based on the change. Run again with the result:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy><name>cy</name> </boy>,
David
shows @XmlAccessorType this annotation.
Change three:
Add another field, int age=10, to boy, i.e.
, on the basis of the second change
Java code
- Package JAXB;
- Import Javax.xml.bind.annotation.XmlAccessorType;
- Import javax.xml.bind.annotation.XmlRootElement;
- Import Javax.xml.bind.annotation.XmlAccessType;
- @XmlRootElement
- @XmlAccessorType (Xmlaccesstype.property)
- Public class Boy {
- String name = "CY";
- int age = 10;
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
Obviously, this age is not translated into an XML file. The solution is:
Java code
- Import Javax.xml.bind.annotation.XmlAccessorType;
- Import javax.xml.bind.annotation.XmlElement;
- Import javax.xml.bind.annotation.XmlRootElement;
- Import Javax.xml.bind.annotation.XmlAccessType;
- @XmlRootElement //Bixude
- @XmlAccessorType (Xmlaccesstype.property)
- Public class Boy {
- String name = "CY";
- @XmlElement
- int age = 10;
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
Add @XmlElement Annotation. The result of the operation is:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><boy><age>10</age> <name>CY </name></boy>
David
For the root element, you can set properties:
@XmlRootElement (name= "B" namespace= "Http://test")
In this way, the,<boy> tag becomes the <b> tag in the generated XML file. and add a namespace.
The following explains the role of @XmlJavaTypeAdaptor:
Java code
- @XmlRootElement
- @XmlAccessorType (Xmlaccesstype.property)
- Public class Boy {
- private String name = "CY";
- private address address; //Is an interface
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- this.name = name;
- }
- }
When the Java object is converted to XML, the interface address cannot be converted.
So add @XmlJavaTypeAdapter here (Addressadapter.class)
So write one more Addressadaptor class.
This class returns an object of a concrete implementation class for the address interface.
This is the role of @XmlJavaTypeAdapter.
"Turn-and-organize" JAXB annotations @XmlRootElement and XML file parsing