Thursday, 10 December 2015

Writing a XML file Using XmlSerializer in Android with SHAIKHRIYAZ.

                    Android has a special class XmlSerializer to write data in XML format . You can read everything about XmlSerializer here : 

http://developer.android.com/reference/org/xmlpull/v1/XmlSerializer.html

                                           In this post we are going to learn how to use xmlSerializer and it methods to create xml file so lets do that:

File testfile=new 
File(Environment.getExternalStorageDirectory(),"rawxml.xml");
try {
    FileOutputStream fos=new FileOutputStream(testfile,false);
    XmlSerializer serializer = Xml.newSerializer();
    serializer.setOutput(fos, "utf-8");
    serializer.startDocument("UTF-8",null);
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);
    //write start tag <hello>
    serializer.startTag("","hello");
    // value
    serializer.text("shaikhriyaz");
    //write end tag </hello>
    serializer.endTag("","hello");
    serializer.endDocument();
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}