问题 创建xml文件并将其保存在内部存储android中


我想检查android内部存储如果 new.xml 存在(将由我创建)然后它应该为我返回一个句柄,我可以很容易地向它添加新数据。如果它不存在,则创建一个新数据并向其添加数据。

我的xml文件结构就像这样简单。

<root>
    <record>a</record>
    <record>b</record>
    <record>c</record>
</root>

如果文件在那里我只会向它添加一条新记录。但是如果不是我将创建一个新文件并将第一条记录添加到它。

以及我如何能够以一种方式读取这些数据 arraylist。代码的一个例子非常感谢提前。


2550
2017-07-27 11:42


起源



答案:


这很简单。这会对你有所帮助:

  String filename = "file.txt";

    FileOutputStream fos;       

    fos = openFileOutput(filename,Context.MODE_APPEND);


    XmlSerializer serializer = Xml.newSerializer();
    serializer.setOutput(fos, "UTF-8");
    serializer.startDocument(null, Boolean.valueOf(true));
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

    serializer.startTag(null, "root");

    for(int j = 0 ; j < 3 ; j++)
    {

        serializer.startTag(null, "record");

        serializer.text(data);

        serializer.endTag(null, "record");
    }
     serializer.endDocument();

     serializer.flush();

     fos.close();

使用DOM解析器读回数据:

    FileInputStream fis = null;
    InputStreamReader isr = null;

    fis = context.openFileInput(filename);
    isr = new InputStreamReader(fis);
    char[] inputBuffer = new char[fis.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fis.close();

    /*
     * converting the String data to XML format
     * so that the DOM parser understand it as an XML input.
     */
        InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));

        ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();

    XmlData xmlDataObj;
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    NodeList items = null;
    Document dom;

    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();
    dom = db.parse(is);
    // normalize the document
    dom.getDocumentElement().normalize();

    items = dom.getElementsByTagName("record");

    ArrayList<String> arr = new ArrayList<String>();

    for (int i=0;i<items.getLength();i++){

        Node item = items.item(i);

         arr.add(item.getNodeValue());

    }               

13
2017-07-27 14:47



所以没有 serializer.endTag(null, "root"); ? - Dan Chaltiel


答案:


这很简单。这会对你有所帮助:

  String filename = "file.txt";

    FileOutputStream fos;       

    fos = openFileOutput(filename,Context.MODE_APPEND);


    XmlSerializer serializer = Xml.newSerializer();
    serializer.setOutput(fos, "UTF-8");
    serializer.startDocument(null, Boolean.valueOf(true));
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

    serializer.startTag(null, "root");

    for(int j = 0 ; j < 3 ; j++)
    {

        serializer.startTag(null, "record");

        serializer.text(data);

        serializer.endTag(null, "record");
    }
     serializer.endDocument();

     serializer.flush();

     fos.close();

使用DOM解析器读回数据:

    FileInputStream fis = null;
    InputStreamReader isr = null;

    fis = context.openFileInput(filename);
    isr = new InputStreamReader(fis);
    char[] inputBuffer = new char[fis.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fis.close();

    /*
     * converting the String data to XML format
     * so that the DOM parser understand it as an XML input.
     */
        InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));

        ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();

    XmlData xmlDataObj;
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    NodeList items = null;
    Document dom;

    dbf = DocumentBuilderFactory.newInstance();
    db = dbf.newDocumentBuilder();
    dom = db.parse(is);
    // normalize the document
    dom.getDocumentElement().normalize();

    items = dom.getElementsByTagName("record");

    ArrayList<String> arr = new ArrayList<String>();

    for (int i=0;i<items.getLength();i++){

        Node item = items.item(i);

         arr.add(item.getNodeValue());

    }               

13
2017-07-27 14:47



所以没有 serializer.endTag(null, "root"); ? - Dan Chaltiel


使用 getFilesDir() 获取可以创建文件的路径。通过创建文件对象来检查文件是否存在 File newXml = new File(getFilesDir+"/new.xml")。然后检查它是否存在使用 if(newXml.exists())

要解析数据,请参阅 开发文档 。正如您在XmlPullParser示例中所看到的,它可以返回一个列表。


0
2017-07-27 11:54



如何将数据附加到现有文件? - Mj1992
在问这里之前请好好研究一下。检查 stackoverflow.com/questions/5059296/... - Aswin Kumar
thnx的帮助。 - Mj1992


使用 openFileOutput 在内部存储和创建文件 getFileStreamPath 检查它们是否已存在并与它们一起工作

要将数据读入数组,只需打开文件流并使用任何适当的XML解析器


0
2017-07-27 12:06



如何将数据附加到现有文件 - Mj1992
通过getFileStreamPath获取File,检查它是否存在,打开FileOutputStream并将数据写入流 - Dmitriy Tarasov