在xml文件中保存对象列表

时间:2014-03-05 11:27:22

标签: android

我想将mylist(包含Sms)保存在xml文件中

private String checkSms() {

Uri uriSMSURI = Uri.parse("content://sms/inbox");  

ArrayList<Sms> myList = new ArrayList<Sms>();

Cursor cur = getActivity().getContentResolver().query(uriSMSURI, null, null,null, null);
while (cur.moveToNext()) 
{
   newSms.setsender(cur.getString(2));
   for(int i = 0 ; i < myList.size(); i++){
      mylist.add(newSms;)}
   }
    System.out.println(sms);
    return (myList); 
}

3 个答案:

答案 0 :(得分:1)

这样的事情会创建一个新的XML文件,其列表以XML格式保存:

XmlSerializer serializer = Xml.newSerializer();
StringWriter writer = new StringWriter();

try {
    serializer.setOutput(writer);
    serializer.startDocument("UTF-8", true);
    serializer.startTag("", "messages");
    serializer.attribute("", "number", String.valueOf(messages.size()));
    for(String text : myList) {
        serializer.startTag("", "sms");
        serializer.text(text);
        serializer.endTag("", "sms");
    }
    serializer.endTag("", "messages");
    serializer.endDocument();
    String xml = writer.toString();
} catch (Exception e) {
    throw new RuntimeException(e);
}

try {
    File root = new File(Environment.getExternalStorageDirectory(), "List");
    if(!root.exists()) root.mkdirs();
    File file = new File(root, name);
    FileWriter writer = new FileWriter(file);
    writer.append(xml);
    writer.flush();
    writer.close();
    Toast.makeText(this, "Saved list", Toast.LENGTH_SHORT).show();
} catch(IOException e) {
    e.printStackTrace();
}

您还需要写入权限,因此请将其添加到清单文件中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:1)

以下是解决方案:

private ArrayList<Sms> checkSms() {
    Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getActivity().getContentResolver().query(uriSMSURI, null, null, null, null);
    while (cur.moveToNext()) 
    {Sms newSms = new Sms();
         newSms.setsender(cur.getString(2));
         newSms.setbody(cur.getString(13));
         myList.add(newSms);
    }   
    XmlTools();
    return(myList);
}

// And I added a method XmlTools() to create the XML file 
private void XmlTools() {
    File newxmlfile = new File(Environment.getExternalStorageDirectory()+ "/SmsFile.xml");
    try
    {Log.v(BackupFragment.this.getClass().getName(), "create file:" + newxmlfile.createNewFile());} 
    catch (IOException e)
    {Log.e("IOException", "exception in createNewFile() method");}
    FileOutputStream fileos = null;
    try 
    {fileos = new FileOutputStream(newxmlfile);}
    catch (FileNotFoundException e) 
    {Log.e("FileNotFoundException", "can't create FileOutputStream");}
    XmlSerializer serializer = Xml.newSerializer();
    try {
        serializer.setOutput(fileos, "UTF-8");
        serializer.startDocument(null, Boolean.valueOf(true));

        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);
         serializer.startTag("", "Document");
        for(int i = 0 ; i < myList.size(); i++) {
            serializer.startTag("", "sms");
            serializer.startTag(null, "sender");
            serializer.text( myList.get(i).getsender());
            serializer.endTag(null, "sender");
             serializer.startTag(null, "body");
             serializer.text(myList.get(i).getbody());
            serializer.endTag(null, "body");
            serializer.endTag("", "sms");
        }
         serializer.endTag("", "Document");
        serializer.endDocument();
        serializer.flush();
        fileos.close();
    } catch (Exception e) {
        Log.e("Exception", "error occurred while creating xml file");
    }
}

答案 2 :(得分:0)

试试这样的话。 但首先要记住将库添加到项目中,可以从here下载。

@Root
private class Sms{ //simple sms class you can have different but remember to add @Root and @Element adnotations
@Element
private String smsContent;
  public String getSmsContent(){
   return smsContent;
  }
}


@Root("SmsList")
private class SmsList{
   @ElementList
   private List<Sms> smsList;
   public void setSmsList(List<Sms> smsList){
     this.list= smsList;
   }

}

            Serializer serializer = new Persister();

            SmsList smsList = new SmsList();
            smsList.setSmsList(myList);

            File f = getFile(xml);
            serializer.write(smsList,f);

并且显然打开了一些输出流来保存文件中的xml - string。

将文件保存在android中添加到清单。

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

然后

public File getFile(){
    String filename = "xmlFile.xml";

    try {
      File file = new File(Environment.getExternalStorageDirectory() + File.separator + fileName);
      return file;
    } catch (Exception e) {
      e.printStackTrace();
    }
 }

有关更多示例,请查看here