Android Appending to an XML file (school Project)
<p>I have looked through many of the Append XML questions, but they dont seem to apply.  I can append to my file but with each entry I get the root and all the beginning stuff I don't need.  I am using serializer.startDocument and serializer.endDocument like one had suggested.  I am struggleing with how to code this properly</p>

<pre><code>package edu.rasmussen.mobile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;


public class TreasureActivity extends Activity {

private static final String FILENAME = "data.xml";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_treasure);
    ImageButton cancelBtn =  (ImageButton)findViewById(R.id.cancelBtn1);
    Button btnSave =  (Button)findViewById(R.id.btnSave);

    btnSave.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // this gets the data entered so that it can be put into the xml file
            EditText nameText = (EditText) findViewById(R.id.editName);
            EditText clue1Text = (EditText) findViewById(R.id.edit1clue);
            EditText clue2Text = (EditText) findViewById(R.id.edit2clue);
            EditText clue3Text = (EditText) findViewById(R.id.edit3clue);
            EditText answerText = (EditText) findViewById(R.id.editAnswer);
            EditText locationText = (EditText) findViewById(R.id.editLocation);
            EditText pointsText = (EditText) findViewById(R.id.editPoints);

            String sName = nameText.getText().toString();
            String sClue1 = clue1Text.getText().toString();
            String sClue2 = clue2Text.getText().toString();
            String sClue3 = clue3Text.getText().toString();
            String sAnswer = answerText.getText().toString();
            String sLocation = locationText.getText().toString();
            String sPoints = pointsText.getText().toString();

            //Write Data to a File in XML Format
            try {
                FileOutputStream mOutput = openFileOutput(FILENAME, Activity.MODE_APPEND);
                //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 &lt;?xml declaration with encoding (if encoding not null) and standalone flag (if standalone 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");

                        /*
                        &lt;Items&gt;
                            &lt;treasure&gt;
                                &lt;name&gt;Example1&lt;/name&gt;
                                &lt;clue1&gt;MyClue&lt;/clue1&gt;
                                &lt;clue2&gt;MyClue2&lt;/clue2&gt;
                                &lt;clue3&gt;MyClue3&lt;/clue3&gt;
                                &lt;answer&gt;myAnswer&lt;/answer&gt;
                                &lt;pointValue&gt;5&lt;/pointValue&gt;
                            &lt;/treasure&gt;

                            &lt;treasure&gt;
                                &lt;name&gt;Example2&lt;/name&gt;
                                &lt;clue1&gt;MyClue&lt;/clue1&gt;
                                &lt;clue2&gt;MyClue2&lt;/clue2&gt;
                                &lt;clue3&gt;MyClue3&lt;/clue3&gt;
                                &lt;answer&gt;myAnswer&lt;/answer&gt;
                                &lt;pointValue&gt;15&lt;/pointValue&gt;
                            &lt;/treasure&gt;
                        &lt;/Items&gt;
                         */


                    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();
            }




            // Return back to the MainActivity Window
            finish();
            startActivity(new Intent(getBaseContext(), MainActivity.class));

        }

    });



    cancelBtn.setOnClickListener(new View.OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        {
            finish();
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.help_activity, menu);
    return true;
}

}
</code></pre>

<p>How do I get it to append only the new stuff being entered so that my file looks like this</p>

<pre><code>&lt;Items&gt;
                        &lt;treasure&gt;
                            &lt;name&gt;Example1&lt;/name&gt;
                            &lt;clue1&gt;MyClue&lt;/clue1&gt;
                            &lt;clue2&gt;MyClue2&lt;/clue2&gt;
                            &lt;clue3&gt;MyClue3&lt;/clue3&gt;
                            &lt;answer&gt;myAnswer&lt;/answer&gt;
                            &lt;pointValue&gt;5&lt;/pointValue&gt;
                        &lt;/treasure&gt;

                        &lt;treasure&gt;
                            &lt;name&gt;Example2&lt;/name&gt;
                            &lt;clue1&gt;MyClue&lt;/clue1&gt;
                            &lt;clue2&gt;MyClue2&lt;/clue2&gt;
                            &lt;clue3&gt;MyClue3&lt;/clue3&gt;
                            &lt;answer&gt;myAnswer&lt;/answer&gt;
                            &lt;pointValue&gt;15&lt;/pointValue&gt;
                        &lt;/treasure&gt;
                    &lt;/Items&gt;
</code></pre>

<p>Right now it looks like the Following, but I thought that the serializer would keep it parsed correctly.</p>

<pre><code>&lt;Items&gt;
    &lt;treasure&gt;
        &lt;name&gt;Example1&lt;/name&gt;
        &lt;clue1&gt;MyClue&lt;/clue1&gt;
        &lt;clue2&gt;MyClue2&lt;/clue2&gt;
        &lt;clue3&gt;MyClue3&lt;/clue3&gt;
        &lt;answer&gt;myAnswer&lt;/answer&gt;
        &lt;pointValue&gt;5&lt;/pointValue&gt;
    &lt;/treasure&gt;
&lt;/Items&gt;
&lt;Items&gt;
    &lt;treasure&gt;
        &lt;name&gt;Example1&lt;/name&gt;
        &lt;clue1&gt;MyClue&lt;/clue1&gt;
        &lt;clue2&gt;MyClue2&lt;/clue2&gt;
        &lt;clue3&gt;MyClue3&lt;/clue3&gt;
        &lt;answer&gt;myAnswer&lt;/answer&gt;
        &lt;pointValue&gt;5&lt;/pointValue&gt;
    &lt;/treasure&gt;
&lt;/Items&gt;
</code></pre>
