android - XML:JAXB Mapping Java Objects and XML Document -
i'm trying implement java classes handling following xml code snippet:
<party date="2012-09-30"> <guest name="albert"> <drink>wine</drink> </guest> </party>
i've wrote 3 classes:
party.java:
package li.mnet.www.java.xml; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlrootelement; @xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name = "party") public class party { @xmlattribute(name = "date") private string partydate; public party() {} public string getpartydate() {return partydate;} public void setpartydate(string partydate) { this.partydate = partydate; } }
guest.java:
package li.mnet.www.java.xml; import javax.xml.bind.annotation.xmlelement; public class guests { private string name; public guests() {} public void setguestname(string name) {this.name = name;} @xmlelement(name = "name") public string getguestname() {return name;} }
partyrunner.java:
package li.mnet.www.java.xml; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.marshaller; public class partyrunner { public static void main(string[] args) { party party = new party(); guests guest = new guests(); party.setpartydate("2012-09-03"); guest.setguestname("albert"); jaxbcontext context; try { context = jaxbcontext.newinstance(party.class); marshaller m = context.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, boolean.true); m.marshal(party, system.out); } catch (jaxbexception e) {e.printstacktrace();} } }
after running application following console output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <party date="2012-09-03"/>
what have change, class guest.java gets printed out too?
thanks lot support!
when you're trying marshal data through jaxb, you'll give instance of class (here party) , traverse object , of attributes , tries serialize them final output using hints provided jaxb annotations.
remember jaxb ignores properties of class has no jaxb annotation. can tell whether generate xml element
or xml attribute
given class property. can use these annotations on properties or getter
methods.
in example, want have guest
element inside party
element. in main
method (in partyrunner
), you're marshaling instance of party
class, class has no reference guests
class. creating instance of guests
class wouldn't enough. have create logical relationship between 2 classes , annotate them make possible generate appropriate xml. party
class should this:
import javax.xml.bind.annotation.*; @xmlaccessortype(xmlaccesstype.field) @xmlrootelement(name = "party") public class party { @xmlattribute(name = "date") private string partydate; @xmlelement(name="guest") private guests guests; public party() {} public string getpartydate() {return partydate;} public void setpartydate(string partydate) { this.partydate = partydate; } public guests getguests() { return guests; } public void setguests(guests guests) { this.guests = guests; } }
if run partyrunner
again you'll have in output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <party> <partydate>2012-09-03</partydate> <guest> <name>albert</name> </guest> </party>
as can see, there inner element named name
in guests
element. due annotation you've specified getguestname
method in guests
class. in order make jaxb generate xml attribute property (instead of xml element), need change jaxb annotation in guests
class too. have annotated getguestname
method @xmlelement
. cause generate xml element. if change @xmlattribute
, run partyrunner
again, you'll have in output:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <party date="2012-09-03"> <guest name="albert"/> </party>
also in sample xml have drink
element inside guests
property. same true drink
property. drink
string
property in guests
class annotated @xmlattribute(name = "drink")
. final guests
class generate xml mentioned @ beginning of question should this:
import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlelement; public class guests { private string name; private string drinktype; public guests(){} public void setguestname(string name) {this.name = name;} @xmlattribute(name = "name") public string getguestname() {return name;} @xmlelement(name = "drink") public string getdrinktype() { return drinktype; } public void setdrinktype(string drinktype) { this.drinktype = drinktype; } }
and partyrunner
should initialize drink
property wine
:
public static void main(string[] args) { party party = new party(); guests guest = new guests(); party.setpartydate("2012-09-03"); guest.setguestname("albert"); guest.setdrinktype("wine"); party.setguests(guest); jaxbcontext context; try { context = jaxbcontext.newinstance(party.class); marshaller m = context.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, boolean.true); m.marshal(party, system.out); } catch (jaxbexception e) {e.printstacktrace();} }
Comments
Post a Comment