how can we merge two java objects in one xml file? -
this program. correct way merge 2 objects?
public class objecttoxml { public static void main(string[] args) throws exception{ jaxbcontext contextobj = jaxbcontext.newinstance(employee.class); marshaller marshallerobj = contextobj.createmarshaller(); marshallerobj.setproperty(marshaller.jaxb_formatted_output, true); try{ employee employees = new employee(); employees.setemployee(new arraylist<employee>()); employee emp1=new employee(1,"vimal jaiswal",50000); employee emp2=new employee(2,"kamal",40000); employees.getemployee().add(emp1); employees.getemployee().add(emp2); marshallerobj.marshal(employees, new fileoutputstream("e:\\employee.xml")); } catch(jaxbexception e){ system.out.println(e); }}}
its giving output doulbe times:
1,"vimal jaiswal",50000 2,"kamal",40000 1,"vimal jaiswal",50000 2,"kamal",40000
please find below code solve problem,
package com.mss.sample; import javax.xml.bind.annotation.xmlelement; /** * employee model class * * @author anilkumar bathula */ public class employee { // fields int empid; string empname; int empsal; public employee(int empid, string empname, int empsal) { super(); this.empid = empid; this.empname = empname; this.empsal = empsal; } @xmlelement public int getempid() { return empid; } public void setempid(int empid) { this.empid = empid; } @xmlelement public string getempname() { return empname; } public void setempname(string empname) { this.empname = empname; } @xmlelement public int getempsal() { return empsal; } public void setempsal(int empsal) { this.empsal = empsal; } }
serialize class:
package com.mss.sample; import java.util.arraylist; import java.util.list; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; /** * employees class * * @author anilkumar bathula */ @xmlrootelement public class employees { private list<employee> employees; public employees() { employees = new arraylist<employee>(); } @xmlelement public list<employee> getemployees() { return employees; } public void setemployees(list<employee> employees) { this.employees = employees; } }
mainclass of generating xml:
package com.mss.sample; import java.io.fileoutputstream; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.marshaller; import com.mss.sample.employees; /** * marshal class * * @author anilkumar bathula */ public class marshal { public static void main(string[] args) throws exception { jaxbcontext contextobj = jaxbcontext.newinstance(employees.class); marshaller marshallerobj = contextobj.createmarshaller(); marshallerobj.setproperty(marshaller.jaxb_formatted_output, true); try { employees employees = new employees(); employee emp1 = new employee(1, "vimal jaiswal", 50000); employee emp2 = new employee(2, "kamal", 40000); employees.getemployees().add(emp1); employees.getemployees().add(emp2); marshallerobj.marshal(employees, new fileoutputstream( "d:\\employee.xml")); } catch (jaxbexception e) { system.out.println(e); } } }
Comments
Post a Comment