Jaxb XML to Java object/Java to XML, jaxb XML Object Structure

Source: Internet
Author: User
Tags xml example
    //  Convert XML into Bean      Public   Static  Requestxml parsetobean (string xmlstr) {requestxml = Null  ;  Try  {Jaxbcontext = Jaxbcontext. newinstance (requestxml. Class  ); Unmarshaller um = Jaxbcontext. createunmarshaller (); requestxml = (Requestxml) Um. unmarshal (New  Bytearrayinputstream (xmlstr. getbytes ()));}  Catch  (Jaxbexception e) {e. getmessage ();}  Return  Requestxml ;}  //  Convert bean to XML      Private   Static String parsetoxml (Object JavaBean) Throws  Exception {jaxbcontext Context =Jaxbcontext. newinstance (xmlbean. getclass (); extends aller m = Context. createmarshaller (); stringwriter SW = New  Stringwriter (); M. Marshal (JavaBean, SW );  Return  Sw. tostring ();} 

Jaxb structure:

XML example:

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Bean  ID = "10"  Code  = "Xm1000x"  >      <  Name  > X. M <  Name  >          <  Levels  >          <  Level  Key  = "1" > None </  Level  >          <  Level  Key  = "2"  > Nothing </  Level  >      </  Levels  >          <  Extras >          <  Extra  >              <  Type  > 1 </  Type  >              <  Content  > XX. Mm. xx </  Content  >          </ Extra  >          <  Extra  >              <  Type  > 2 </  Type  >              <  Content  > XX. Mm. xx </  Content  >         </  Extra  >      </  Extras  >      </  Bean  >         

Java Bean:

Import Java. util. arraylist; import Java. util. list; import javax. XML. BIND. annotation. xmlattribute; import javax. XML. BIND. annotation. xmlelement; import javax. XML. BIND. annotation. xmlelementwrapper; import javax. XML. BIND. annotation. xmlrootelement; @ xmlrootelement (name = "Bean") public class bean {private integer ID; private string code; private string name; private list <level> levels = new arraylist <level> (); Private list <Extra> extras = new arraylist <Extra> (); Public Bean () {}@ xmlattributepublic integer GETID () {return ID ;}@ xmlattributepublic string getcode () {return code ;}@ xmlelementpublic string getname () {return name ;} @ xmlelementwrapper (name = "levels") @ xmlelement (name = "level") public list <level> getlevels () {return levels;} @ xmlelementwrapper (name = "EXTRAS ") @ xmlelement (name = "extra") public list <Extra> getextras () {return extras;} public void setid (integer ID) {This. id = ID;} public void setcode (string code) {This. code = Code;} public void setname (string name) {This. name = Name;} public void setlevels (list <level> levels) {This. levels = levels;} public void setextras (list <Extra> extras) {This. extras = extras ;}}

 Import  Javax. xml. Bind. annotation. xmlattribute;  Import  Javax. xml. Bind. annotation. xmlrootelement;  Import  Javax. xml. Bind. annotation. xmlvalue; @ xmlrootelement  Public   Class  Level {  Private  Integer key;  Private  String value;  Public Level () {}@ xmlattribute (name = "Key" )  Public  Integer getkey (){  Return  Key ;}@ xmlvalue  Public  String getvalue (){  Return  Value ;}  Public   Void  Setkey (integer key) {key = Key ;} Public   Void  Setvalue (string value ){  This . Value = Value ;}} 
 Import  Javax. xml. Bind. annotation. xmlelement;  Import  Javax. xml. Bind. annotation. xmlrootelement; @ xmlrootelement  Public   Class  Extra {  Private  String type; Private  String content;  Public  Extra () {}@ xmlelement  Public  String GetType (){  Return  Type ;}@ xmlelement  Public  String getcontent (){  Return  Content ;}  Public   Void Settype (string type ){  This . Type = Type ;}  Public   Void  Setcontent (string content ){  This . Content = Content ;}} 

 

 

 

Related Article

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.