Java loop inside Unmarshal object -


at link: jaxb unmarshalling xml string - looping through tags proposed solution. i'm trying benefit not able have working in case.

consider xml this:

<campaign value="field1">   <name value="firstname"/>   <type value="type"/>   <record>     <firsttime value="13"/>     <secondtime value="14"/>   </record> </campaign> 

and consider classes unmarshalling, created , working. campaign class, containing name, value, record[] etc.

jaxbcontext c = jaxbcontext.newinstance(campaign.class); unmarshaller u = c.createunmarshaller(); campaign campaign = (campaign) u.unmarshal(file); 

i can extract value of "name" , "type" but, because of list<>, not able go beyond this.

private string name = null; [...] name = campaign.getname().getvalue(); system.out.println(name); 

how loop <record>, , values firsttime , secondtime, knowing there more <record> in other xml file?

edit: campaign class

@xmlrootelement(name = "campaign") @xmltype(name = "campaign", proporder = {     "name",     "type",     "record" }) public class campaign {     @xmlelement(required = true)     protected name name;      @xmlelement(required = true)     protected type type;      @xmlelement(required = true)     protected list<record> record= new arraylist<record>();       public name getname () {return name;}     public void setname (name value) {this.name= value;}      public type gettype () {return type;}     public void settype (type value) {this.type = value;}      public void setrecord(recordvalue) {this.record.add(value);} } 

record class

@xmlrootelement(name = "record") @xmltype(name = "record", proporder = {     "firsttime",     "secondtime" }) public class record{     @xmlelement(required = true)     protected firsttime firsttime;      @xmlelement(required = true)     protected secondtime secondtime;       public name getfirsttime () {return firsttime;}     public void setfirsttime (firsttime value) {this.firsttime= value;}      public name getsecondtime () {return secondtime;}     public void setsecondtime (secondtime value) {this.secondtime= value;} } 

the program more big, keeps repeating.. need extract in order upload db later. now, need print them on screen , knowing have values i'd need.

yes had @ link too, don't know why can't concentrate on how continue, seems worst part easier me!

thanks

edit 3: i forgot paste methods set , get.

@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "type") public class type {      @xmlattribute     protected string value;      public string getvalue() {return value;}     public void setvalue(string value) {this.value = value;} } 

it same all, pasted type here.

edit 4: resolution able find solution, not 1 proposed , marked correct. although helped me creating getrecord() getter.

without changing code, adding record.class:

public list<record> getrecord() { return this.record; } 

and main class:

for (int i=1; i<=campaign.getrecord().size(); i++)   {                system.out.println(campaign.getrecord().get(i-1).getfirsttime().getvalue()); system.out.println(campaign.getrecord().get(i-1).getsecondtime().getvalue()); } 

i able print "13" , "14", more in general, able print inside record , if record have list inside, 1 more:

for (int j=1; j<= campaign.getrecord().get(i-1).getanewlistinsiderecord().size(); j++) { system.out.println(campaign.getrecord().get(i-1).getanewlistinsiderecord().get(j-1).getaobject().getvalue()); } 

will the job.

-1 because starts 0.

i'm still unsure problem is. tell me if answer not you're looking for.

you have few changes make :

@xmlrootelement(name = "campaign") @xmltype(name = "campaign", proporder = {     "name",     "type",     "record" }) public class campaign {     @xmlelement(required = true)     protected name name;      @xmlelement(required = true)     protected type type;      @xmlelement(required = true)     protected list<record> record;       public name getname () {return name;}     public void setname (name value) {this.name= value;}      public type gettype () {return type;}     public void settype (type value) {this.type = value;}      //initialise record list inside getter instead of in member declaration     public list<record> getrecord() {         if(this.record == null) record = new arraylist<>();         return this.record;     }      //do not use setter add method list     public void setrecord(list<record> value) {this.record = value;}      //if need add record inside list, not use setter, define add method or access list getter.     public void addrecord(record value) {this.record.add(value);} } 

i'll go under assumption have defined proper tostring() method of classes used in campaign.

for exemple, record class string may go :

@override public string tostring(){     return " first value : + "this.firsttime.getvalue() + " second value : " +this.secondtime.getvalue(); } 

now display :

list<file> files = new arraylist<>();  //add xml files containing campaign root element files list jaxbcontext c = jaxbcontext.newinstance(campaign.class); unmarshaller u = c.createunmarshaller();  //declare list store of camapaign object list<campaign> campaigns = new arraylist<>();  for(file f : files) {     campaigns.add(u.unmarshall(f)); }  //display campaigns for(campaign camp : campaigns){     system.out.println(camp.getname());     system.out.println(camp.gettype());      //display records     for(record rec : camp.getrecord()){         system.out.println(rec);     } } 

you can of course change system.out.println() lines whatever code want.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -