Java Android 创建 xml 并将其写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18423650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:30:58  来源:igfitidea点击:

Android creating and writing xml to file

javaandroidxmlfileserializer

提问by Selli S

I'm taking a course on android app development and trying to create and write an xml file to the internal storage on the android. I am having issues with how to set this up initially, as far as the methods. I've written most of it but have errors that I can't figure out. Maybe because I've been working on this all day, I don't know. Here's my code for this class. Errors I'm getting are illegal modifiers on public String treasures and FileOutputStream. Any help would be appreciated.

我正在学习有关 android 应用程序开发的课程,并尝试创建一个 xml 文件并将其写入 android 上的内部存储。就方法而言,我在最初如何设置它时遇到了问题。我已经写了大部分,但有我无法弄清楚的错误。也许是因为我整天都在做这个,我不知道。这是我的这个类的代码。我得到的错误是公共字符串宝藏和 FileOutputStream 上的非法修饰符。任何帮助,将不胜感激。

Ok, I figured out the initial problem, needed to use try/catch. Was able to run and everything worked fine until I got to the save file. Getting an error now:

好的,我想出了最初的问题,需要使用 try/catch。能够运行并且一切正常,直到我到达保存文件。现在出现错误:

SoundPool error loading/system./media./audio./ui/KeypressReturn.ogg.  
AudioService Soundpool could not load file: /system/media/audio/ui/KeypressReturnj.ogg

This comes right after the "file created" is written to the log. I'm guessing it's trying to write to the wrong file? Need it to write to /data/data. There is no audio in my app. I've added the new code below:

这是在“创建的文件”写入日志后立即出现的。我猜它正在尝试写入错误的文件?需要它写入/data/data。我的应用程序中没有音频。我在下面添加了新代码:

Old Code:

旧代码:

    public void onSaveTreasureClick(View v) throws FileNotFoundException{
        Log.v("SaveTreasure","Button was clicked");
        File f = new File(getFilesDir(),"treasure.xml");
        FileOutputStream myFile=openFileOutput(f);
        Log.v("WriteFile","file created");  


        private FileOutputStream openFileOutput(File f) {
            // TODO Auto-generated method stub
            return null;
        }



    public String treasures(Treasure treasure) throws Exception{

        XmlSerializer xmlSerializer = Xml.newSerializer();
        StringWriter write = new StringWriter();
        final EditText tres=(EditText) findViewById(R.id.treasureNametxt);
        String treasureName=tres.getText().toString();
        final EditText c1=(EditText) findViewById(R.id.clue1Txt);
        String clue1=c1.getText().toString();
        final EditText c2=(EditText) findViewById(R.id.clue2Txt);
        String clue2=c2.getText().toString();
        final EditText c3=(EditText) findViewById(R.id.clue3Txt);
        String clue3=c3.getText().toString();
        final EditText ans=(EditText) findViewById(R.id.answerTxt);
        String answer = ans.getText().toString();
        final EditText loc =(EditText) findViewById(R.id.locationTxt);
        String location = loc.getText().toString();
        final EditText pv=(EditText) findViewById(R.id.pointValueTxt);
        String pointValue=pv.getText().toString();

        xmlSerializer.setOutput(write);
    //start Document
        xmlSerializer.startDocument("UTF-8",true);
    //open tag <items>
        xmlSerializer.startTag("", "Items");
        xmlSerializer.startTag("", "Treasures");

        xmlSerializer.startTag("", "TreasureName");
        xmlSerializer.attribute("", TreasureName, treasureName);
        xmlSerializer.endTag("", "TreasureName");

        xmlSerializer.startTag("", "Clue1");
        xmlSerializer.attribute("", "Clue1", clue1);
        xmlSerializer.endTag("", "Clue1");

        xmlSerializer.startTag("", "Clue2");
        xmlSerializer.attribute("", "Clue2", clue2);
        xmlSerializer.endTag("", "Clue2");

        xmlSerializer.startTag("", "Clue3");
        xmlSerializer.attribute("", "Clue3", clue3);
        xmlSerializer.endTag("", "Clue3");

        xmlSerializer.startTag("", "answer");
        xmlSerializer.attribute("", "answer", answer);
        xmlSerializer.endTag("","answer");

        xmlSerializer.startTag("", "location");
        xmlSerializer.attribute("", "location", location);
        xmlSerializer.endTag("", "location");

        xmlSerializer.startTag("", "Points");
        xmlSerializer.attribute("", "PointValue", pointValue);
        xmlSerializer.endTag("", "Points");

        xmlSerializer.endTag("","Treasures");
        xmlSerializer.endTag("", "Items");

        xmlSerializer.endDocument();

        return treasure.toString();

    }

    }  

}

New Code:

新代码:

public void onSaveTreasureClick(View v) throws FileNotFoundException, SAXException{
        Log.v("SaveTreasure","Button was clicked");
        File f = new File(getFilesDir(),"treasure.xml");
        FileOutputStream myFile=openFileOutput(f);
        Log.v("WriteFile","file created");  


    //  private FileOutputStream openFileOutput(File f) {
            // TODO Auto-generated method stub
        //  return null;
    //  }


try{
    final String treasures;

        XmlSerializer xmlSerializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        final EditText tres=(EditText) findViewById(R.id.treasureNametxt);
        String treasureName=tres.getText().toString();
        final EditText c1=(EditText) findViewById(R.id.clue1Txt);
        String clue1=c1.getText().toString();
        final EditText c2=(EditText) findViewById(R.id.clue2Txt);
        String clue2=c2.getText().toString();
        final EditText c3=(EditText) findViewById(R.id.clue3Txt);
        String clue3=c3.getText().toString();
        final EditText ans=(EditText) findViewById(R.id.answerTxt);
        String answer = ans.getText().toString();
        final EditText loc =(EditText) findViewById(R.id.locationTxt);
        String location = loc.getText().toString();
        final EditText pv=(EditText) findViewById(R.id.pointValueTxt);
        String pointValue=pv.getText().toString();

        xmlSerializer.setOutput(writer);
    //start Document
        xmlSerializer.startDocument("UTF-8",true);
    //open tag <items>
        xmlSerializer.startTag("", "Items");
        xmlSerializer.startTag("", "Treasures");

        xmlSerializer.startTag("", "TreasureName");
        xmlSerializer.attribute("", treasureName, treasureName);
        xmlSerializer.endTag("", "TreasureName");

        xmlSerializer.startTag("", "Clue1");
        xmlSerializer.attribute("", "Clue1", clue1);
        xmlSerializer.endTag("", "Clue1");

        xmlSerializer.startTag("", "Clue2");
        xmlSerializer.attribute("", "Clue2", clue2);
        xmlSerializer.endTag("", "Clue2");

        xmlSerializer.startTag("", "Clue3");
        xmlSerializer.attribute("", "Clue3", clue3);
        xmlSerializer.endTag("", "Clue3");

        xmlSerializer.startTag("", "answer");
        xmlSerializer.attribute("", "answer", answer);
        xmlSerializer.endTag("","answer");

        xmlSerializer.startTag("", "location");
        xmlSerializer.attribute("", "location", location);
        xmlSerializer.endTag("", "location");

        xmlSerializer.startTag("", "Points");
        xmlSerializer.attribute("", "PointValue", pointValue);
        xmlSerializer.endTag("", "Points");

        xmlSerializer.endTag("","Treasures");
        xmlSerializer.endTag("", "Items");

        xmlSerializer.endDocument();

        writer.toString();
                myFile.write(writer.toString().getBytes());

}
catch (FileNotFoundException e) {
    System.err.println("FileNotFoundException: " + e.getMessage());
    throw new SAXException(e);

} catch (IOException e) {
    System.err.println("Caught IOException: " + e.getMessage());
}

}

采纳答案by Selli S

I figured this one out. I was using the attribute tag incorrectly. Come to find out, I didn't even need to use it. The correct way was to do the following:

我想通了这一点。我错误地使用了属性标签。来了解一下,我什至不需要使用它。正确的方法是执行以下操作:

xmlSerializer.startTag("", "TreasureName");
xmlSerializer.text(treasureName);
xmlSerializer.endTag("","TreasureName");

After changing all of them, I was able to create the file and write the data to it.

在更改所有这些之后,我能够创建文件并将数据写入其中。