Android and Xml Serialization with Simple
Xml serialization is almost becoming a standard requirement for a language these days and so as I have been taking an Android class and I couldn’t find an Xml Serialization library as part of Android by default, I set out in search of one.
I came across a java XML Serialization project called Simple.
So here is a quick entry-level example of how to use Simple in an Android development project.
Note: This walk-thru assumes you are using Eclipse.
Step 1 – Create a new Android Project
- Go to File | New Project and select Android.
- Provide a Project Name.
- Select the minimum build target.
- Provide a Package name.
- Click Finish.
Step 2 – Download Simple
- Go to the Simple download page: http://simple.sourceforge.net/download.php
- Extract the zip file.
Step 3 – Add the Simple library to your project
- Create a folder called libs in your project.
- Copy the jar file called simple-xml-2.6.2.jar to the libs directory you just created.Note: Be aware your version may be newer than 2.6.2.
- In Eclipse, right-click on simple-xml-2.6.2.jar (if it doesn’t show up refresh) and choose Build Path | Add to Build Path.
Step 4 – Create an Serializeable object
- Right-click on your package and choose New | Class.
- Provide a class name and click ok.
- The following is an example Person class:Person.java
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
package
org.jaredbarneck.cs6890;
import
org.simpleframework.xml.Element;
import
org.simpleframework.xml.Root;
@Root
public
class
Person
{
public
Person()
{
}
public
Person(String inFirstName, String inLastName)
{
SetFirstname(inFirstName);
SetLastname(inLastName);
}
@Element
private
String FirstName;
public
String GetFirstName()
{
return
FirstName;
}
public
void
SetFirstname(String inFirstName)
{
FirstName = inFirstName;
}
@Element
private
String LastName;
public
String GetLastName()
{
return
LastName;
}
public
void
SetLastname(String inLastName)
{
LastName = inLastName;
}
@Override
public
boolean
equals(Object inObject)
{
if
(inObject
instanceof
Person)
{
Person inPerson = (Person)inObject;
return
this
.FirstName.equalsIgnoreCase(inPerson.FirstName)
&&
this
.LastName.equalsIgnoreCase(inPerson.LastName);
}
return
false
;
}
}
Step 5 – Serialize and Deserialize in your main Activity
- Add the following code to your main Activity:Note: Code should be clear and is commented.PersonActivity.java
0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
package
org.jaredbarneck.cs6890;
import
java.io.File;
import
org.simpleframework.xml.Serializer;
import
org.simpleframework.xml.core.Persister;
import
android.app.Activity;
import
android.os.Bundle;
public
class
PersonActivity
extends
Activity
{
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a Person object
Person person1 =
new
Person(
"John"
,
"Johnson"
);
// Create a file to save to and make sure to use the path provided from
// getFilesDir().getPath().
File xmlFile =
new
File(getFilesDir().getPath() +
"/Person.xml"
);
// Serialize the Person
try
{
Serializer serializer =
new
Persister();
serializer.write(person1, xmlFile);
}
catch
(Exception e)
{
e.printStackTrace();
}
// Create a second person object
Person person2 =
null
;
// Deserialize the Person
if
(xmlFile.exists())
{
try
{
Serializer serializer =
new
Persister();
person2 = serializer.read(Person.
class
, xmlFile);
}
catch
(Exception e)
{
e.printStackTrace();
}
}
boolean
b = person1.equals(person2);
}
}
Go ahead and try this in your Android emulator and step through it with a debugger.
You have now successfully implemented Xml Serialization in Java on Android using Simple.
roadside assistance gloucester
Rhyous
Hey Rhyous, Is it possible for you to look into this: http://stackoverflow.com/q/41426251/1944896 ?
Currently, person.xml file is created with root tag . How to create as a root tag(means first letter with capital)?
I also want to include namespace with root tag. For example -
Is it possible to achieve with "Simple" library?
If i have a list of persons. How would i loop through the xml elements and put the read values in a arraylist, i mean add to arraylist the below object:
person2 = serializer.read(Person.class, xmlFile)
Where should I put my xml file ? and what will I do when I need to read the xml from thhp api ? thank
Same problem here.
Use DDMS