java - Complete list of items are not showing up after deserializing XML String into object using Jackson XML mapper -
my input xml string contains list of entries. when deserialize object using jackson xmlmapper see 1 item in list coming. parent elements have been defined generic objects in pojo.
xml string (itemlist contains 3 items) :
<msg> <head> <client>myclient</client> <routingarea>test</routingarea> <serverid>abcxyz123</serverid> </head> <body> <userdetailresponse> <userdetail> <customer> <customerid>1456342711975</customerid> <businessunit>test0000</businessunit> <name> <salutation>u</salutation> <first>tropical tan</first> </name> <privacy>y</privacy> </customer> <itemlist> <count>3</count> <item> <serviceidentifier>000001</serviceidentifier> <billingidentifier>000001</billingidentifier> </item> <item> <serviceidentifier>000002</serviceidentifier> <billingidentifier>000002</billingidentifier> </item> <item> <serviceidentifier>000003</serviceidentifier> <billingidentifier>000003</billingidentifier> </item> </itemlist> </userdetail> </userdetailresponse> </body> </msg>
java code:
private final static xmlmapper mapper = new xmlmapper(); public static <t> t getxmlobject(string xml, class<t> cls) throws ioexception { return mapper.readvalue(xml, cls); } public static void main(string[] args) throws exception { string xmlstring = "<msg><head><client>myclient</client><routingarea>test</routingarea><serverid>abcxyz123</serverid></head><body><userdetailresponse><userdetail><customer><customerid>1456342711975</customerid><businessunit>test0000</businessunit><name><salutation>u</salutation><first>tropical tan</first></name><privacy>y</privacy></customer><itemlist><count>3</count><item><serviceidentifier>000001</serviceidentifier><billingidentifier>000001</billingidentifier></item><item><serviceidentifier>000002</serviceidentifier><billingidentifier>000002</billingidentifier></item><item><serviceidentifier>000003</serviceidentifier><billingidentifier>000003</billingidentifier></item></itemlist></userdetail></userdetailresponse></body></msg>"; jacksonxmlmodule jacksonxmlmodule = new jacksonxmlmodule(); jacksonxmlmodule.setdefaultusewrapper(false); myresponse myresponse = getxmlobject(xmlstring, myresponse.class); system.out.println("xml object: \n" + myresponse.tostring()); objectmapper mapper = new objectmapper(); string str = mapper.writerwithdefaultprettyprinter().writevalueasstring(myresponse); system.out.println("json : \n" + str); }
pojo:
public class myresponse { private object head; private object body; public object gethead() { return head; } public void sethead(object head) { this.head = head; } public object getbody() { return body; } public void setbody(object body) { this.body = body; } }
though there 3 items under itemlist in input xml string result object contains 3rd item.
result:
json : { "head" : { "client" : "myclient", "routingarea" : "test", "serverid" : "abcxyz123" }, "body" : { "userdetailresponse" : { "userdetail" : { "customer" : { "customerid" : "1456342711975", "businessunit" : "test0000", "name" : { "salutation" : "u", "first" : "tropical tan" }, "privacy" : "y" }, "itemlist" : { "count" : "3", "item" : { "serviceidentifier" : "000003", "billingidentifier" : "000003" } } } } } }
your example not work without declaring actual types of head
, body
. plain object
cause body
bound map
, , repeated elements of same name, last 1 ends retained. underlying problems xml has no distinction between arrays , objects (unlike json), , way resolve difference information java classes. need have list
or array type bind things arrays.
Comments
Post a Comment