简体   繁体   English

Android创建和编写xml到文件

[英]Android creating and writing xml to file

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. 我正在学习Android应用程序开发课程,并尝试创建并编写一个xml文件到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. 我得到的错误是公共字符串宝藏和FileOutputStream上的非法修饰符。 Any help would be appreciated. 任何帮助,将不胜感激。

Ok, I figured out the initial problem, needed to use try/catch. 好的,我想出了使用try / catch所需的初始问题。 Was able to run and everything worked fine until I got to the save file. 能够运行,一切正常,直到我到达保存文件。 Getting an error now: 现在出错:

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. 需要它写入/ data / data。 There is no audio in my app. 我的应用中没有音频。 I've added the new code below: 我在下面添加了新代码:

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

}

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. 更改完所有内容后,我就可以创建文件并将数据写入其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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