Question

I have written a very simple XML file that should be saved in the data folder under file explorer but I cannot find it at all! I am not sure if my code is buggy or something's wrong with my eclipse. Here is the code below.

package com.example.xmltesterone;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    Button SubmitButton, ViewDataButton;
    EditText FirstNameBox, LastNameBox;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SubmitButton = (Button) findViewById(R.id.SubmitButton);
        ViewDataButton = (Button) findViewById(R.id.ViewData_Button);

        FirstNameBox = (EditText) findViewById(R.id.EditTextOne);
        final String FN = FirstNameBox.getText().toString();
        LastNameBox = (EditText) findViewById(R.id.EditTextTwo);

        SubmitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    FileOutputStream myFile = openFileOutput("treasures.xml", Activity.MODE_APPEND);
                    XmlSerializer serializer = Xml.newSerializer();

                    serializer.setOutput(myFile, "UTF-8");

                    serializer.startDocument(null, Boolean.valueOf(true));


                    // set indentation option 
                    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

                    // start a new top level tag and other tags
                    serializer.startTag(null, "Items");
                    serializer.startTag(null, "Treasure");


                    serializer.startTag(null, "FirstName");
                    serializer.attribute(null, "xxxx", FN);
                    serializer.endTag(null, "FirstName");

                    serializer.endDocument();

                    // perform the write by flushing
                    serializer.flush();

                    // close the file stream
                    myFile.close();



                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

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

}
Était-ce utile?

La solution

openFileOutput points to folder getFilesDir(). In most cases it's /data/data/<your app>/files. But unless you have rooted device, you have no access to this folder from File Explorer on your device. But you can always take a look on this folder via DDMS which can be found at Android SDK.

Autres conseils

just simple question : why not use dom for xml or saxparser ( cant comment so asking it here )

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top