我试图创建一个xml文件,然后稍后在
上添加一些节点。我已经成功创建了xml文件,但是添加新节点是一个问题
我正在使用此代码创建xml
try {
String out = "userData.xml";
FileOutputStream fos = new FileOutputStream(out);
//FileOutputStream fileos= mcontext.openFileOutput(fos, Context.MODE_PRIVATE);
FileOutputStream fileos = new FileOutputStream (new File(out), true); // true will be same as Context.MODE_APPEND
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null, "userData");
xmlSerializer.startTag(null, "userName");
xmlSerializer.text("testting");
xmlSerializer.endTag(null, "userName");
xmlSerializer.startTag(null,"password");
xmlSerializer.text("paswordtesting");
xmlSerializer.endTag(null, "password");
xmlSerializer.endTag(null, "userData");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite = writer.toString();
fileos.write(dataWrite.getBytes());
fileos.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(mcontext, "IllegalStateException" + e, Toast.LENGTH_SHORT).show();
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(mcontext, "IllegalStateException", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(mcontext, "IOException", Toast.LENGTH_SHORT).show();
}
}
创建一个像这样的xml文件
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<userData>
<userName>testting</userName>
<password>paswordtesting</password>
</userData>
现在我要向该xml文件添加新节点 使用此代码
public void testXMLFiles(Context mContext) {
//create a new file called "new.xml" in the SD card
final File newXmlFile = new File("userData.xml");
RandomAccessFile randomAccessFile = null;
final boolean fileExists = newXmlFile.exists();
String lastLine = null;
if (fileExists) {
try {
randomAccessFile = new RandomAccessFile(newXmlFile, "rw");
randomAccessFile.seek(0);
if (null != randomAccessFile) {
final Scanner scanner = new Scanner(newXmlFile);
int lastLineOffset = 0;
int lastLineLength = 0;
while (scanner.hasNextLine()) {
// +1 is for end line symbol
lastLine = scanner.nextLine();
lastLineLength = lastLine.length() + 2;
lastLineOffset += lastLineLength;
}
// don't need last </root> line offset
lastLineOffset -= lastLineLength;
// got to string before last
randomAccessFile.seek(lastLineOffset);
}
} catch(FileNotFoundException e) {
Log.e("FileNotFoundException", "can't create FileOutputStream");
} catch (IOException e) {
Log.e("IOException", "Failed to find last line");
}
} else {
try {
newXmlFile.createNewFile();
} catch(IOException e) {
Log.e("IOException", "exception in createNewFile() method");
}
try {
randomAccessFile = new RandomAccessFile(newXmlFile, "rw");
} catch(FileNotFoundException e) {
Log.e("FileNotFoundException", "can't create FileOutputStream");
}
}
//we create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
if (randomAccessFile == null) {
return;
}
try {
final StringWriter writer = new StringWriter();
serializer.setOutput(writer);
if (!fileExists) {
serializer.startDocument(null, true);
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "userData");
} else {
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
}
serializer.startTag(null, "child1");
serializer.endTag(null, "child1");
serializer.startTag(null, "child2");
serializer.attribute(null, "attribute", "value");
serializer.endTag(null, "child2");
serializer.startTag(null, "child3");
serializer.text("some text inside child3");
serializer.endTag(null, "child3");
if (!fileExists) {
serializer.endTag(null, "userData");
}
serializer.flush();
if (lastLine != null) {
serializer.endDocument();
writer.append(lastLine);
}
// Add \n just for better output in console
randomAccessFile.writeBytes(writer.toString() + "\n");
randomAccessFile.close();
Toast.makeText(mContext, "Save!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
e.printStackTrace();
}
}
我得到的投入是
<child1 />
<child2 attribute="value" />
<child3>some text inside child3</child3>
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<userData><userName>testting</userName>
<password>paswordtesting</password>
</userData>
我想要的是
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<userData>
<userName>testting</userName>
<password>paswordtesting</password>
<child1 />
<child2 attribute="value" />
<child3>some text inside child3</child3>
</userData>
如何获得正确的投放位置