Getting xml element name from unmarshalled java object with JAXB -


i have fields annotated @xmlelement(name="xxx") in java model.

is there way xml element name programatically?

say have annotated entity

 @xmlrootelement  public class product {       string name;              @xmlelement(name="sss")       public void setname(string name) {            this.name = name;       } } 

code below print "sss" using java reflection api. here 'product' object of product class

import java.lang.reflect.method; ... method m = product.getclass().getmethod("setname",string.class); xmlelement = m.getannotation(xmlelement.class); string namevalue = a.name(); system.out.println(namevalue); 

if need @xmlelement annotation attribute private field, use this:

field namefield = product.getclass().getdeclaredfield("name"); namefield.setaccessible(true); xmlelement = namefield.getannotation(xmlelement.class); string namevalue = a.name(); system.out.println(namevalue); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -