netbeans - How to read elements of arraylist of a class defined in another class in java? -
i working on project college. have 2 classes "email_info" , "contacts". in class "contacts", made arraylist of type "email_info". class contacts used add data xml file("contacts.xml" ), , uses variables of email_info class. problem whenever try access elements of "contacts.xml" file after unmarshalling file, address "mailwidgetaa.email_info@12d3a4e9" instead of actual data( should like, e_id- abc@gmail.com, pass- password). how do actual data ?
below full code::
package mailwidgetaa; @xmlrootelement public class contacts { list<email_info> contaclist = new arraylist<email_info>(); @xmlelement public list<email_info> getcontaclist() { return contaclist; } public void setcontaclist(list<email_info> contaclist) { this.contaclist = contaclist; } } @xmlrootelement class email_info { string e_id; string u_name; string pass; @xmlelement public string gete_id() { return e_id; } public void sete_id(string e_id) { this.e_id = e_id; } @xmlelement public string getu_name() { return u_name; } public void setu_name(string u_name) { this.u_name = u_name; } @xmlelement public string getpass() { return pass; } public void setpass(string pass) { this.pass = pass; } } public class mailwidgetaa { public static void main(string[] args) { contatcs con = new contacts(); con = null; try { jaxbcontext jaxbc1 = jaxbcontext.newinstance(contacts.class); unmarshaller unmarsh = jaxbc1.createunmarshaller(); con = (contacts) unmarsh.unmarshal(new file("contacts.xml")); } catch (exception e) { system.out.println("exception " + e.getmessage()); } email_info ein = new email_info(); ein.sete_id(ui); ein.setu_name(un); con.getcontaclist().add(ein); try { jaxbcontext jaxbc = jaxbcontext.newinstance(contacts.class); marshaller marsh = jaxbc.createmarshaller(); marsh.setproperty(marshaller.jaxb_formatted_output, true); marsh.marshal(con, new file("contacts.xml")); } catch (jaxbexception e) { system.out.println("exception" + e.getmessage()); } } iterator e= con.contaclist.iterator(); while(e.hasnext()){ system.out.println(e.next()); } }
that's because haven't implemented tostring method in email_info class. implement like:
@override public string tostring() { return "e_id: " + e_id + " u_name: " + u_name + " pass: " + pass; }
Comments
Post a Comment