Android XML文件资源管理器文件将无法保存

时间:2014-03-01 09:12:07

标签: android xml fileoutputstream

我编写了一个非常简单的XML文件,应保存在文件资源管理器下的数据文件夹中,但我根本找不到它!我不确定我的代码是否有错误或者我的日食出了什么问题。这是下面的代码。

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

}

2 个答案:

答案 0 :(得分:1)

只是一个简单的问题:为什么不将dom用于xml或saxparser(无法评论,因此请在此处提问)

答案 1 :(得分:1)

openFileOutput指向文件夹getFilesDir()。在大多数情况下,它是/data/data/<your app>/files。但除非您使用root设备,否则您无法从设备上的文件资源管理器访问此文件夹。但您可以随时通过DDMS查看此文件夹,该文件夹可在Android SDK找到。