java - How to use JAXB to add a custom "type" attribute to my XML? -


this jaxb problem 1 bean class

  @xmlaccessortype(xmlaccesstype.field)   @xmltype(name = "tablebean")    public class tablebean {   @xmlattribute   private string type;    @xmlelement(name = "created_at")   private date created_at;    @xmlelement(name = "database_id")   private int database_id;    @xmlelement(name = "id")   private int id; 

i want xml this

<tables>   <table>      <created_at type="datetime">2013-08-28t21:14:35+09:00</created_at>      <database_id type="integer">1</database_id>      <id type="integer">1</id>  <table> <tables> 

i have try make class this

public class type_int {  private string type; private int id; @xmlattribute public string gettype() {     return type; } public void settype(string type) {     this.type = type; } @xmlvalue public int getid() {     return id; } public void setid(int id) {     this.id = id; } 

use@xmlattribute in type_int.class can want,but project have many variables,i write classes everyone,so wheather can somthing in main bean.class,and can make easily

if want map field like:

private int id; 

to xml element following:

<id type="integer">123</id> 

then leverage xmladapter. leveraging type_int class question create class following declaration , implement required marshal , unmarshal methods convert integer to/from type_int.

public class intadapter extends xmladapter<type_int, integer> {     ... } 

to leverage adapter need change field primitive type int object type integer , annotate as:

@xmljavatypeadapter(intadapter.class) private integer id; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -