Save the list of objects in an XML file

I would like to save mylist(containing Sms) in an XML file

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); 
}
+4
source share
3 answers

Something like this will create a new XML file with your list saved in XML format:

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

You will also need write permission, so add this to the manifest file:

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

Here is the solution:

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");
    }
}
+1
source

. , .

@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- .

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

, , .

0

Source: https://habr.com/ru/post/1530234/


All Articles