"Turn-and-organize" JAXB annotations @XmlRootElement and XML file parsing

Source: Internet
Author: User

Java code
    1. @Retention (Value=runtime)
    2. @Target (Value=type)
    3. Public @interface Xmlrootelement
    4. @Inherited
    5. @Retention (Value=runtime)
    6. @Target (Value={package,type})
    7. 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
  1. Package JAXB;
  2. Import Javax.xml.bind.annotation.XmlAccessorType;
  3. Import javax.xml.bind.annotation.XmlRootElement;
  4. Import Javax.xml.bind.annotation.XmlAccessType;
  5. @XmlRootElement //Must be marked with this element
  6. @XmlAccessorType (Xmlaccesstype.field)
  7. Public class Boy {
  8. String name = "CY";
  9. }
  10. Package JAXB;
  11. Import Java.io.StringReader;
  12. Import Javax.xml.bind.JAXBContext;
  13. Import javax.xml.bind.JAXBException;
  14. Import Javax.xml.bind.Marshaller;
  15. Import Javax.xml.bind.Unmarshaller;
  16. Public class Jaxbtest {
  17. public static void Main (string[] args) throws jaxbexception {
  18. Jaxbcontext context = Jaxbcontext.newinstance (boy.   Class);
  19. Marshaller Marshaller = Context.createmarshaller ();
  20. Unmarshaller Unmarshaller = Context.createunmarshaller ();
  21. Boy boy = new boy ();
  22. Marshaller.marshal (Boy, System.out);
  23. System.out.println ();
  24. String xml = "<boy><name>David</name></boy>";
  25. Boy Boy2 = (boy) Unmarshaller.unmarshal (new StringReader (XML));
  26. System.out.println (Boy2.name);
  27. }
  28. }


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
  1. Package JAXB;
  2. Import Javax.xml.bind.annotation.XmlAccessorType;
  3. Import javax.xml.bind.annotation.XmlRootElement;
  4. Import Javax.xml.bind.annotation.XmlAccessType;
  5. @XmlRootElement
  6. @XmlAccessorType (Xmlaccesstype.property)
  7. Public class Boy {
  8. String name = "CY";
  9. int age = 10;
  10. Public String GetName () {
  11. return name;
  12. }
  13. public void SetName (String name) {
  14. this.name = name;
  15. }
  16. }


Obviously, this age is not translated into an XML file. The solution is:

Java code
  1. Import Javax.xml.bind.annotation.XmlAccessorType;
  2. Import javax.xml.bind.annotation.XmlElement;
  3. Import javax.xml.bind.annotation.XmlRootElement;
  4. Import Javax.xml.bind.annotation.XmlAccessType;
  5. @XmlRootElement //Bixude
  6. @XmlAccessorType (Xmlaccesstype.property)
  7. Public class Boy {
  8. String name = "CY";
  9. @XmlElement
  10. int age = 10;
  11. Public String GetName () {
  12. return name;
  13. }
  14. public void SetName (String name) {
  15. this.name = name;
  16. }
  17. }



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
  1. @XmlRootElement
  2. @XmlAccessorType (Xmlaccesstype.property)
  3. Public class Boy {
  4. private String name = "CY";
  5. private address address; //Is an interface
  6. Public String GetName () {
  7. return name;
  8. }
  9. public void SetName (String name) {
  10. this.name = name;
  11. }
  12. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.