Xml Serialization in Java using Simple – Inheritance
This is a continuation from this post: Xml Serialization in Java using Simple
Example 4 – Serializing a list of objects that inherit from Person
Lets create some objects that inherit from Person. I looked at some documentation to try to get it right the first time. And then I hoped that it would just work….
import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class Person { @Element(name="FirstName") private String _FirstName = ""; @Element(name="LastName") private String _LastName = ""; public String getFirstName() { return _FirstName; } public void setFirstName(String inFirstName) { _FirstName = inFirstName; } public String getLastName() { return _LastName; } public void setLastName(String inLastName) { _LastName = inLastName; } }
import java.util.ArrayList; import java.util.List; import org.simpleframework.xml.ElementList; import org.simpleframework.xml.Root; @Root public class People { @ElementList(inline=true) List<Person> List = new ArrayList<Person>(); }
I didn’t want to get too complex so I only added a single item to Patient, a list of Symptoms.
import java.util.ArrayList; import java.util.List; import org.simpleframework.xml.ElementList; public class Patient extends Person { @ElementList(entry = "Symptom", inline = true) public List<String> Symptoms = new ArrayList<String>(); }
For the Physician, again to keep it simple, I only added a list of Hospitals.
import java.util.ArrayList; import java.util.List; import org.simpleframework.xml.ElementList; public class Physician extends Person { @ElementList(entry = "Hospital", inline = true) public List<String> Hospitals = new ArrayList<String>(); }
And here is the main method where I create the instances and serialize them.
import java.io.File; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; public class Run { public static void main(String[] args) throws Exception { People people = new People(); Patient p1 = new Patient(); p1.setFirstName("Jared"); p1.setLastName("Barneck"); p1.Symptoms.add("Runny nose"); p1.Symptoms.add("Congestion"); people.List.add(p1); Physician p2 = new Physician(); p2.setFirstName("Mike"); p2.setLastName("Michaels"); p2.Hospitals.add("Intermount Health Care"); p2.Hospitals.add("St. Marks"); people.List.add(p2); Serializer serializer = new Persister(); File file = new File("people.xml"); serializer.write(people, file); } }
And yeah! This worked. Here is the Xml.
<people> <person class="Patient"> <FirstName>Jared</FirstName> <LastName>Barneck</LastName> <Symptom>Runny nose</Symptom> <Symptom>Congestion</Symptom> </person> <person class="Physician"> <FirstName>Mike</FirstName> <LastName>Michaels</LastName> <Hospital>Intermount Health Care</Hospital> <Hospital>St. Marks</Hospital> </person> </people>
There you go.
There are a lot more examples here:
Simple Xml Serialization Tutorial
That's what I looked. Thanks a lot. I'm trying to code backup functionality in my application. Your article is very helpful.
[...] in Java using Simple – Inheritance Filed under: FreeBSD — rhyous @ 9:24 pm Read more Share this:DiggRedditLike this:LikeBe the first to like this post. Leave a [...]