멍멍이네 블로그


  
        File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.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");
                        //i indent code just to have a view similar to xml-tree
                                serializer.startTag(null, "child1");
                                serializer.endTag(null, "child1");
                              
                                serializer.startTag(null, "child2");
                                //set an attribute called "attribute" with a "value" for
                                serializer.attribute(null, "attribute", "value");
                                serializer.endTag(null, "child2");
                      
                                serializer.startTag(null, "child3");
                                //write some text inside
                                serializer.text("some text inside child3");
                                serializer.endTag(null, "child3");
                              
                        serializer.endTag(null, "root");
                        serializer.endDocument();
                        //write xml data into the FileOutputStream
                        serializer.flush();
                        //finally we close the file stream
                        fileos.close();
                      
                } catch (Exception e) {
                        Log.e("Exception","error occurred while creating xml file");
                }

 

 

출처 : http://www.shop-wiz.com/document/android/execise_linkage_web_xml_create