2013-02-13 120 views
1

我想通过使用简单的Xml序列化在我的应用程序中序列化一个对象(http://simple.sourceforge.net/home.php)。我试图序列化我的人员类,但是当我在我的设备上运行它时,我找不到我创建的xml文件。请参阅我的代码如下:Android XML序列化

Person类:

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; 
    } 
} 

主要活动:

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(); 

     } 

    } 

} 

任何人都可以看到,我错了?在任何人建议之前,我已经将写入外部存储权限添加到我的清单中。

回答

1
File xmlFile = new File(getFilesDir().getPath() + "/Person.xml"); 

我记得,该方法getFilrsDir()将返回应用程序文件夹路径,这在数据/数据文件夹,你只能找到它,如果你已经植根您的设备,
试试这个:

File xmlFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Person.xml"); 

而且可能还需要在您的Person类的一些annocate:

@root (name = "person") <br> 
public class Person { 

    @Element (entry = "firstname") 
    private String FirstName; 
    @Element (entry = "lastname") 
    private String LastName; 

} 

希望这有助于。

+0

谢谢我试过但没有运气 – DMC 2013-02-14 10:04:53