java - Adding an Attribute to a JAXB Element -
i'm struggling jaxb parsing , need guidance.
essentially, i'm trying add attributes class variables have declared elements using @xmlelement. far, attempt use @xmlattribute sets attribute @ class level.
what i"m getting this:
<dataclass newattribute="test"> <myelement>i wish element had attribute</myelement> <anotherelement>i wish element had attribute too</anotherelement> </dataclass>
i'd to this:
<dataclass> <myelement thisatt="this i'm talking about">this better</myelement> <anotherelement thisatt="a different attribute here">so this</anotherelement> </dataclass>
i've seen other posts add attribute single element using @xmlvalue, doesn't work when have elements, , won't work on multiple elements.
does have thought on how accomplished?
thanks! jason
this create xml:
public class jaxbattributes { public static void main(string[] args) throws exception { marshaller marshaller = jaxbcontext.newinstance(dataclass.class).createmarshaller(); stringwriter stringwriter = new stringwriter(); dataclass dataclass = new dataclass( new foo("this i'm talking about", "this better"), new foo("a different attribute here", "so this")); marshaller.marshal(dataclass, stringwriter); system.out.println(stringwriter); } @xmlrootelement(name = "dataclass") @xmltype(proporder = {"myelement", "anotherelement"}) static class dataclass { private foo myelement; private foo anotherelement; dataclass() {} public dataclass(foo myelement, foo anotherelement) { this.myelement = myelement; this.anotherelement = anotherelement; } public foo getmyelement() { return myelement; } public void setmyelement(foo myelement) { this.myelement = myelement; } public foo getanotherelement() { return anotherelement; } public void setanotherelement(foo anotherelement) { this.anotherelement = anotherelement; } } static class foo { private string thisatt; private string value; foo() {} public foo(string thisatt, string value) { this.thisatt = thisatt; this.value = value; } @xmlattribute public string getthisatt() { return thisatt; } public void setthisatt(string thisatt) { this.thisatt = thisatt; } @xmlvalue public string getvalue() { return value; } public void setvalue(string value) { this.value = value; } } }
Comments
Post a Comment