Save a XML file in Android

edited January 2017 in Android Mode

Hello,

I am trying to save some data on the phone, for when the user closes the app. I thought the best way to do this is via XML (correct me if I am wrong).

So it works fine in Java mode, but when I switch to android it doesn't.

XML xml = loadXML("database.xml");
    XML amount_tot_liter_xml = xml.getChild("amount_tot_liter");
    println(amount_tot_liter_xml.getFloatContent());

The database.xml is inside the data folder. I can read data from the file. But I can't save data:

    xml.removeChild(amount_tot_liter_xml);
    amount_tot_liter_xml = xml.addChild("amount_tot_liter");
    amount_tot_liter_xml.setFloatContent(amount_tot_liter);
    saveXML(xml, "data/database.xml");

Answers

  • @toorgmot===

    using the XML examples from docs (java mode) && modifying it for android (which has a lot of other ways && libs to do that) :: try this code

            import java.io.FileOutputStream;
    
            import android.content.Context;
    
            // The following short XML file called "mammals.xml" is parsed 
            // in the code below. It must be in the project's "data" folder.
            //
            // <?xml version="1.0"?>
            // <mammals>
            //   <animal id="0" species="Capra hircus">Goat</animal>
            //   <animal id="1" species="Panthera pardus">Leopard</animal>
            //   <animal id="2" species="Equus zebra">Zebra</animal>
            // </mammals>
    
            File[] listOfFiles;// this is for showing what happens
            String files; // the same
    
            XML xml;
            XML xml2;
            File mon;
    
    
            void setup() {
              xml = loadXML("mammals.xml");
              XML firstChild = xml.getChild("animal");
              xml.removeChild(firstChild);
             mon = getActivity().getFilesDir();/// INTERNAL storage dir
    
              saveXML(xml, mon + "/subset.xml");
              verifie();
              xml2 = loadXML(mon + "/subset.xml");
              println(xml2);
            };
    
    
    
    
            public void verifie(){// for showing....
    
              String directory = new String(getActivity().getFilesDir().getAbsolutePath() );
    
            File folder = new File(directory);
              listOfFiles = folder.listFiles(); 
    
    
              for (int i = 0; i < listOfFiles.length; i++) 
              {
    
              if (listOfFiles[i].isFile()) 
              {
              files = listOfFiles[i].getName();
    
              System.out.println("i=====" + i + "    " + files);
             }
             }
    
    
    
            }
    
            // Sketch saves the following to a file called "subset.xml":
            // <?xml version="1.0"?>
            // <mammals>
            //   <animal id="1" species="Panthera pardus">Leopard</animal>
            //   <animal id="2" species="Equus zebra">Zebra</animal>
            // </mammals>
    
Sign In or Register to comment.