これを 2 番目のノードに出力するにはどうすればよいですか? 現在、古いデータのすぐ上にあるようです。私のインストラクターはそれがうまくいくはずだと言っていますが、うまくいきません。
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>
そして、2回目以降はこのように見えるはずです。
<?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>