Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

java - 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. 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.

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:

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:

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...