Re: [Simple-support] Custom HashMap serialization/deserialization - Help
Brought to you by:
niallg
|
From: Simon B. <sim...@if...> - 2012-05-28 10:12:56
|
Hi Miguel,
Try the following:
Course.java:
package com.simpleserializer.model;
import org.simpleframework.xml.Attribute;
@Root(name=Course)
public class Course {
@Attribute private String id;
@Attribute private String name;
public Course(String id, String name) {
this.setId(id);
this.setName(name);
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Course2.java:
package com.simpleserializer.model;
import java.util.HashMap;
@Root(name=Courses)
public class Courses {
@ElementList(entry=Course,type=
com.simpleserializer.model.Course.class, inline=true)
private HashMap<String, Course> courses = new
HashMap<String, Course>();
public void put(String courseId, Course course)
{
this.courses.put(courseId, course);
}
}
Von: Miguel Ángel Martínez Fernández [mailto:mig...@gm...]
Gesendet: Montag, 28. Mai 2012 01:35
An: sim...@li...
Betreff: [Simple-support] Custom HashMap serialization/deserialization -
Help
Hi,
I am trying to serialize (and deserialize) two Java clases (Courses.java
and Couse.Java) in order to get the following XML:
<Courses>
<Course id="course01" name="Carnet de conducir A"/>
<Course id="course02" name="Carnet de conducir A"/>
</Courses>
Course.java:
package com.simpleserializer.model;
import org.simpleframework.xml.Attribute;
public class Course {
@Attribute private String id;
@Attribute private String name;
public Course(String id, String name) {
this.setId(id);
this.setName(name);
}
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Course2.java:
package com.simpleserializer.model;
import java.util.HashMap;
public class Courses {
private HashMap<String, Course> courses = new HashMap<String,
Course>();
public void put(String courseId, Course course)
{
this.courses.put(courseId, course);
}
}
And example about how these clases are used is:
Courses courses = new Courses();
Course course1 = new Course("course01", "My first course");
Course course2 = new Course("course02", "My second course");
courses.put(course1.getId(), course1);
courses.put(course2.getId(), course2);
Could you please help me? I would really appreciate it!
Thanks!
|