File newxmlfile = new File("/data/com.itwine/emergency.xml");
try{
newxmlfile.createNewFile();
}catch(IOException e){
Log.e("IOException", "exception in createNewFile() method");
}
//we have to bind the new file with a FileOutputStream
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't create FileOutputStream");
}
//we create a XmlSerializer in order to write xml data
XmlSerializer serializer = Xml.newSerializer();
try {
//we set the FileOutputStream as output for the serializer, using UTF-8 encoding
serializer.setOutput(fileos, "UTF-8");
//Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
serializer.startDocument(null, Boolean.valueOf(true));
//set indentation option
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
//start a tag called "root"
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, "EmailId");
serializer.text(txtemailid.getText().toString());
serializer.endTag(null,"EmailId");
serializer.startTag(null, "PhoneNo");
serializer.text(txtphoneno.getText().toString());
serializer.endTag(null,"PhoneNo");
serializer.endTag(null,"root");
serializer.endDocument();
//write xml data into the FileOutputStream
serializer.flush();
//finally we close the file stream
fileos.close();
Toast.makeText(getApplication(), "xml created",Toast.LENGTH_LONG);
} catch (Exception e) {
Log.e("Exception","error occurred while creating xml file");
}
是的,有一种方法。您可以使用 FileOutputStream 编写 Xmlfile。并使用 XMLserializer 设置属性和标签。看看我给出的示例。