使用openFileOutputそれはかなりありますシンプル。これはあなたを助ける:
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());
}
'serializer.endTag(null、" root "); –