2013-02-13 87 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