One way: string concatenation
StringBuilder sb=new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.append("<root>"); sb.append("<book>android 基础<book>"); sb.append("</root>"); File path = new File(Environment.getExternalStorageDirectory().getPath(), "config.xml"); FileOutputStream fos= new FileOutputStream(path); fos.write(sb.toString().getBytes()); fos.close();
Second way: XmlSerializer
Private void XmlSerializerTest () throws IOException { // 1. XmlSerializer obtain an instance of the class, obtained by XML XmlSerializer Serializer = Xml.newSerializer (); File File = new new File (Environment.getExternalStorageDirectory (), "the config.xml" ); a FileOutputStream fos = new new a FileOutputStream (File); // read stream encoding format serializer.setOutput (fos, "UTF-. 8" ); // 2. write beginning xml, xml header encoding format. 8-UTF serializer.startDocument ( "UTF-. 8", to true ); //3. Write node, the namespace xmlns, which AndroidManifest as defined xmlns serializer.startTag ( null , "root"); // root node start serializer.startTag ( null , "Book"); // Book starting node serializer.text ( "android base" ); serializer.endTag ( null , "Book"); // Book end node serializer.endTag ( null , "the root"); // the root end node // XML end serializer.endDocument (); }