// 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 ;}}