Java annotations on XML, automatic generation of XML Files-@XML * * *

Source: Internet
Author: User
Uses the JDK's own javax.xml.bind.JAXBContext to convert objects and XML strings to and from each other.        If there is a slight restriction on the XML format to be generated, a little control is needed for the object that generates the XML, and one of the most feasible ways to control the object is to use annotations.        (JDK 1.6 api:http://www.cs.uic.edu/~mcpc/java_docs/api/index.html?javax/xml/bind/jaxbcontext.html) A few of the more commonly used: @XmlRootElement: Root node @XmlAttribute: This property is an attribute of XML @XmlElement: This property is an element of XML and can be added to the attribute (name= "newelemen Tname "), then the elment tag of the generated XML string is Newelementname
Example:
Package test;

Import Java.io.StringWriter;
Import Javax.xml.bind.JAXBContext;
Import javax.xml.bind.JAXBException;

Import Javax.xml.bind.Marshaller;
Import Test.bean.EleClassA;
Import Test.bean.EleClassB;

Import Test.bean.RootClass;
		public class Test1 {public static void main (string[] args) {Rootclass rc = new Rootclass ();
		Eleclassa a = new Eleclassa ();
		
		ELECLASSB B = new ELECLASSB ();
		A.SETATTRC ("Attrc");
		A.setelea ("Elea");
		
		A.seteleb ("Eleb");
		B.setattrpassword ("Attrpassword");
		B.setattrusername ("Attrusrname");
		
		B.setelecode ("Elecode");
		Rc.seta (a);
		Rc.setb (b);
		Rc.setroot ("root");
		
		
		Rc.setroota ("Roota");
		Jaxbcontext context;
			try {context = Jaxbcontext.newinstance (Rootclass.class);
			Marshaller mar = Context.createmarshaller ();
			Mar.setproperty (Marshaller.jaxb_formatted_output, true);
			
			Mar.setproperty (marshaller.jaxb_encoding, "UTF-8");
			
			StringWriter writer = new StringWriter ();
			
			Mar.marshal (RC, writer); System.oUt.println (Writer.tostring ());
		catch (Jaxbexception e) {e.printstacktrace ();

}} package Test.bean;
Import javax.xml.bind.annotation.XmlElement;

Import javax.xml.bind.annotation.XmlRootElement;
	@XmlRootElement (name= "Rootclass") public class Rootclass {private Eleclassa A;
	Private ELECLASSB B;
	private String root;
	
	Private String Roota;
	@XmlElement (name= "Eleclassa") public Eleclassa Geta () {return A;
	} public void SetA (Eleclassa a) {this.a = A;
	@XmlElement (name= "Eleclassa") public ELECLASSB Getb () {return b;
	public void Setb (ELECLASSB b) {this.b = b;
	Public String Getroot () {return root;
	public void Setroot (String root) {this.root = root;
	Public String Getroota () {return roota;
	} public void Setroota (String roota) {This.roota = Roota;

}} package Test.bean;
Import Javax.xml.bind.annotation.XmlAttribute;

Import javax.xml.bind.annotation.XmlElement;
	public class Eleclassa {private String Elea; Private String Eleb;

	Private String attrc;
	@XmlElement public String Getelea () {return elea;
	} public void Setelea (String elea) {This.elea = Elea;
	@XmlElement (name= "Elebnewname") public String Geteleb () {return eleb;
	} public void Seteleb (String eleb) {This.eleb = Eleb;
	@XmlAttribute () public String getattrc () {return attrc;
	} public void Setattrc (String attrc) {this.attrc = ATTRC;

}} package Test.bean;
Import Javax.xml.bind.annotation.XmlAttribute;

Import javax.xml.bind.annotation.XmlElement;
	public class Eleclassb {private String attrusername;
	
	Private String Attrpassword;
	
	Private String Elecode;
	@XmlAttribute public String Getattrusername () {return attrusername;
	} public void Setattrusername (String attrusername) {this.attrusername = Attrusername;
	@XmlAttribute (name= "password") public String Getattrpassword () {return attrpassword; } public void Setattrpassword (String attrpassword) {This.attrpassword = AttrpassWOrd;
	@XmlElement public String Getelecode () {return elecode;
	} public void Setelecode (String elecode) {this.elecode = Elecode;
 }
	
	
}

Run the main method in the Test1 class to perform the result:
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<rootclass>
    <eleclassa attrc= "ATTRC" ">
        <eleA>eleA</eleA>
        <elebnewname>eleB</elebnewname>
    </eleClassA>
    <eleclassa attrusername= "Attrusrname" password= "Attrpassword" >
        <elecode>elecode</elecode >
    </EleclassA>
    <root>root</root>
    <rootA>rootA</rootA>
</ Rootclass>


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.