Android generates XML files

Source: Internet
Author: User

There are many articles for parsing XML files, but there are very few articles for generating XML files in Android. By chance, I found a related article and shared it with me:

Xmlserializer is used to write XML files.

private static void XmlFileCreator(List<JokeBean> data){
File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new.xml");
try{
if(!newxmlfile.exists())
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, "jokes");
for(JokeBean joke:data){
serializer.startTag(null, "joke");
//i indent code just to have a view similar to xml-tree
serializer.startTag(null, "id");
serializer.text(joke.getId());
serializer.endTag(null, "id");

serializer.startTag(null, "title");
serializer.text(joke.getTitle());
//set an attribute called "attribute" with a "value" for <child2>
//serializer.attribute(null, "attribute", "value");
serializer.endTag(null, "title");
serializer.startTag(null, "text");
//write some text inside <text>
serializer.text(joke.getText());
serializer.endTag(null, "text");

serializer.endTag(null, "joke");
}
serializer.endTag(null, "jokes");
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");
}
}

In fact, there is another stupid method, but I did not try it: that is, it can solve this problem by writing common character text to a file. However, this is a little more troublesome.

References:

Http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.