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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11687074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:28:02  来源:igfitidea点击:

Create xml file and save it in internal storage android

android

提问by Mj1992

I want to check in the android internal storage if new.xmlexists(which will be created by me) then it should return me a handle for it and i may be easily able to append new data to it.If it doesn't exists create a new one and add data to it.

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

My xml file structure will be simple like this.

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

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

If the file is there I will only add a new record to it.But if doesn't than I will create a new file and add the first record to it.

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

And How I will be able to read this data in an arraylist. An example with code would be great thanks in advance.

以及我如何能够在arraylist. 一个带有代码的示例将非常感谢提前。

回答by Vikram Gupta

It's rather simple. This will help you:

这比较简单。这将帮助您:

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

To read back data using DOM parser:

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

回答by Aswin Kumar

Use getFilesDir()to get the path to where you can create files. Check if the file exists by creating a file object File newXml = new File(getFilesDir+"/new.xml"). Then check if it exists using if(newXml.exists()).

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

To parse the data, refer dev docs . As you can see in the XmlPullParser example, it can return a list.

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

回答by Dmitriy Tarasov

Use openFileOutputto create file in internal storage and getFileStreamPathfor checking if they already exists and working with them

使用openFileOutput在内部存储中创建文件并使用getFileStreamPath检查它们是否已经存在并使用它们

To read data into array just open file stream and use any appropriate XML parser

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