夥計們,我有一個問題,當我們在android中編寫xml時,我的代碼給出了一個異常作爲權限被拒絕。任何人都可以知道它將如何被移除。如何在android中創建xml文件
package com.ex.createXml;
import android.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;
public class createXml extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File newxmlfile = new File("/data/new.xml");
try{
newxmlfile.createNewFile();
}catch(IOException e)
{
Log.e("IOException", "Exception in create new File(");
}
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e)
{
Log.e("FileNotFoundException",e.toString());
}
XmlSerializer serializer = Xml.newSerializer();
try{
serializer.setOutput(fileos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "root");
serializer.startTag(null, "Child1");
serializer.endTag(null, "Child1");
serializer.startTag(null, "Child2");
serializer.attribute(null, "attribute", "value");
serializer.endTag(null, "Child2");
serializer.startTag(null, "Child3");
serializer.text("Some text inside child 3");
serializer.endTag(null,"Child3");
serializer.endTag(null,"root");
serializer.endDocument();
serializer.flush();
fileos.close();
//TextView tv = (TextView)findViewById(R.);
}catch(Exception e)
{
Log.e("Exception","Exception occured in wroting");
}
}
}
請不要再次提出同樣的問題。 – Will 2011-03-03 14:44:44