如何让它输出第二个节点?目前它似乎正好在旧数据之上。我的教练说它应该工作,但它没有。
protected void ModifyXMLFile(String sName, String sClue1, String sClue2,
String sClue3, String sAnswer, String sLocation, String sPoints) {
// Write Data to a File in XML Format
try {
FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_PRIVATE);
//we create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
try {
//we set the FileOutputStream as output for the serialize, using UTF-8 encoding
serializer.setOutput(mOutput, "UTF-8");
//Write <?xml declaration with encoding (if encoding not null) and stand alone flag (if stand alone not null)
serializer.startDocument(null, Boolean.valueOf(true));
//set indentation option
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
//start a tag called "root"
serializer.startTag(null, "Items");
//i indent code just to have a view similar to xml-tree
serializer.startTag(null, "Treasure");
serializer.startTag(null, "Name");
serializer.attribute(null, "Name", sName);
serializer.endTag(null, "Name");
serializer.startTag(null, "Clue1");
serializer.attribute(null, "Clue1", sClue1);
serializer.endTag(null, "Clue1");
serializer.startTag(null, "Clue2");
serializer.attribute(null, "Clue2", sClue2);
serializer.endTag(null, "Clue2");
serializer.startTag(null, "Clue3");
serializer.attribute(null, "Clue3", sClue3);
serializer.endTag(null, "Clue3");
serializer.startTag(null, "Answer");
serializer.attribute(null, "Answer", sAnswer);
serializer.endTag(null, "Answer");
serializer.startTag(null, "Location");
serializer.attribute(null, "Location", sLocation);
serializer.endTag(null, "Location");
serializer.startTag(null, "Points");
serializer.attribute(null, "Points", sPoints);
serializer.endTag(null, "Points");
serializer.endTag(null, "Treasure");
serializer.endTag(null, "Items");
serializer.endDocument();
//write xml data into the FileOutputStream
serializer.flush();
//finally we close the file stream
mOutput.close();
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
}
// mOutput.write(data.getBytes());
// mOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
当前输出如下所示。
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
<Treasure>
<Name Name="p" />
<Clue1 Clue1="l" />
<Clue2 Clue2="m" />
<Clue3 Clue3="o" />
<Answer Answer="k" />
<Location Location="n" />
<Points Points="i" />
</Treasure>
</Items>
第二次通过后它应该看起来像这样。
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
<Treasure>
<Name Name="Treasure 1" />
<Clue1 Clue1="l" />
<Clue2 Clue2="m" />
<Clue3 Clue3="o" />
<Answer Answer="k" />
<Location Location="n" />
<Points Points="i" />
</Treasure>
<Treasure>
<Name Name="Treasure 2" />
<Clue1 Clue1="b" />
<Clue2 Clue2="c" />
<Clue3 Clue3="d" />
<Answer Answer="e" />
<Location Location="f" />
<Points Points="g" />
</Treasure>
</Items>
好的,我添加了以下更改
FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_APPEND);
我还添加了这一行,因为它似乎丢失了。
StringWriter writer = new StringWriter();
当我开始这个时,我得到了同样的东西。一定有更好的方法。
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
<Treasure>
<Name Name="quit" />
<Clue1 Clue1="was" />
<Clue2 Clue2="each" />
<Clue3 Clue3="race" />
<Answer Answer="the" />
<Location Location="life" />
<Points Points="35" />
</Treasure>
</Items><?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<Items>
<Treasure>
<Name Name="zone" />
<Clue1 Clue1="Xbox" />
<Clue2 Clue2="city" />
<Clue3 Clue3="very" />
<Answer Answer="but" />
<Location Location="new" />
<Points Points="12" />
</Treasure>
</Items>