I am trying to Serialize an object in my application by using the Simple Xml Serialization.(http://simple.sourceforge.net/home.php). I am trying to serialize my person class but when I run it on my device I cannot find the xml file I have created. Please see my code below:
Person Class:
public class Person {
public Person() {
}
public Person(String inFirstName, String inLastName) {
SetFirstname(inFirstName);
SetLastname(inLastName);
}
private String FirstName;
public String GetFirstName() {
return FirstName;
}
public void SetFirstname(String inFirstName) {
FirstName = inFirstName;
}
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;
}
}
Main Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Person person1 = new Person("billy", "boontay");
File xmlFile = new File(getFilesDir().getPath() + "/Person.xml");
try {
Serializer serializer = new Persister();
serializer.write(person1, xmlFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Can anyone see where I am going wrong? And before anyone suggests it I have already added the write external storage permission to my manifest.
As I remember, the method getFilrsDir() will return the app folder path, which in data/data folder and you can only find it if you have rooted your device,
try this:
And may be you also need some annocate in your Person class:
Hope this help.