Android创建和编写xml到文件

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

好的,我想出了使用try / catch所需的初始问题。 能够运行,一切正常,直到我到达保存文件。 立即收到错误:

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

这是在“文件创建”写入日志之后。 我猜它试图写错了文件? 需要它写入/ data / data。 我的应用中没有音频。 我在下面添加了新代码:

旧代码:

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

新代码:

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

我想出了这一个。 我错误地使用了属性标记。 来发现,我甚至不需要使用它。 正确的方法是执行以下操作:

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

更改完所有内容后,我就可以创建文件并将数据写入其中。