Skip to content

Instantly share code, notes, and snippets.

@marharrUK
Created July 21, 2011 09:30
Show Gist options
  • Select an option

  • Save marharrUK/1096848 to your computer and use it in GitHub Desktop.

Select an option

Save marharrUK/1096848 to your computer and use it in GitHub Desktop.
Serializing an ArrayList using the Simple Framework for XML
package com.softsols.xmltest;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.Root;
@Default
@Root(name="Job")
public class Job extends MessageType {
private String jobCode;
private String assetCode;
private String description;
public Job(String jobCode, String assetCode, String description) {
super();
this.jobCode = jobCode;
this.assetCode = assetCode;
this.description = description;
}
public String getJobCode() {
return jobCode;
}
public void setJobCode(String jobCode) {
this.jobCode = jobCode;
}
public String getAssetCode() {
return assetCode;
}
public void setAssetCode(String assetCode) {
this.assetCode = assetCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.softsols.xmltest;
import org.simpleframework.xml.Default;
@Default
public class Job extends MessageType {
private String jobCode;
private String assetCode;
private String description;
public Job(String jobCode, String assetCode, String description) {
super("JobDS");
this.jobCode = jobCode;
this.assetCode = assetCode;
this.description = description;
}
public String getJobCode() {
return jobCode;
}
public void setJobCode(String jobCode) {
this.jobCode = jobCode;
}
public String getAssetCode() {
return assetCode;
}
public void setAssetCode(String assetCode) {
this.assetCode = assetCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.softsols.xmltest;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Order;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Transient;
@Root
@Order(attributes={"DataSet", "Version"})
public class MessageType {
@Transient
public String dataSet;
@Attribute(name="Version")
private static final String version = "3.5.1.0";
public MessageType(@Transient String dataSet) {
// TODO Auto-generated constructor stub
this.dataSet = dataSet;
}
@Attribute(name="DataSet")
public String getDataSet(){
return this.dataSet;
}
@Attribute(name="DataSet")
public void setDataSet(String dataSet){
this.dataSet = dataSet;
}
}
public String convertObjectToXML(Object obj){
Serializer serializer = new Persister();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
serializer.write(obj, baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.getMessage();
}
return baos.toString();
}
package com.softsols.xmltest;
import java.util.ArrayList;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.ElementListUnion;
import org.simpleframework.xml.Root;
//<wrapperList>
// <asset>
// <assetCode>air01</assetCode>
// <description>Air Conditioner</description>
// </asset>
// <job>
// <assetCode>air01</assetCode>
// <description>fix it dummy</description>
// <jobCode>0001</jobCode>
// </job>
//</wrapperList>
@Default
@Root
public class WrapperList {
@ElementListUnion({
@ElementList(entry="asset", inline=true, type=Asset.class),
@ElementList(entry="job", inline=true, type=Job.class)
})
private ArrayList<MessageType> list;
public WrapperList( @ElementListUnion({
@ElementList(name="asset", type=Asset.class),
@ElementList(name="job", type=Job.class)
}) ArrayList<MessageType> list){
this.list = list;
}
public WrapperList(MessageType listItem){
this.list = new ArrayList<MessageType>();
this.list.add(listItem);
}
public void addList(MessageType listItem){
this.list.add(listItem);
}
public ArrayList<MessageType> getList(){
return this.list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment