我正在将数据写入SD卡上的xml文件。我希望在根节点在xml中关闭之前附加数据。有人可以帮我弄明白怎么做吗?
我正在使用RandomAccessFile在xml doc中写入数据,但它似乎是在根节点打开后立即附加数据,我想在根节点关闭之前插入数据。我以为(my_file_path.length()) -1)
会在根节点关闭之前接受指针但不会。
我非常感谢您对此的意见。谢谢。
if(xml_file.exists()) {
StringWriter writer2;
String my_file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/App" + "/AppData.xml";
RandomAccessFile raf = new RandomAccessFile(my_file_path, "rw");
raf.seek((my_file_path.length()) -1);
XmlSerializer xmlSerializer5 = Xml.newSerializer();
writer2 = new StringWriter();
xmlSerializer5.setOutput(writer2);
xmlSerializer5.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
xmlSerializer5.startTag(null, "data");
xmlSerializer5.startTag(null, "name");
xmlSerializer5.text("Lisa Doe");
xmlSerializer5.endTag(null, "name");
xmlSerializer5.endTag(null, "data");
xmlSerializer5.flush();
String my_dataWrite = writer2.toString();
raf.write(my_dataWrite.getBytes());
raf.close();
}
我希望我的xml文件看起来像这样:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<root>
<data>
<name>John Doe</name>
</data>
<data>
<name>Lisa Doe</name>
</data>
</root>
但是它将数据附加到文件的开头:
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<root>
<data>
<name>Lisa Doe</name>
</data>
<data>
<name>John Doe</name>
</data>
</root>