简体   繁体   中英

Android XML File explorer file won't save

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

}

只是简单的问题:为什么不使用dom for xml或saxparser (无法评论,所以在这里问一下)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM