Generate an XML file in Android

Source: Internet
Author: User

Generating an XML file in Android is really simple, here are 2 methods, one for writing to a file via string, and the other for XML XmlSerializer.

After you encounter Android write XML content will not be confused

1. Writing files by string

String name=metname.gettext (). toString (); String age=metage.gettext (). toString (); String id=metid.gettext (). toString (); File file=new file (Environment.getexternalstoragedirectory (), "Info.xml");try{     fileoutputstream fos=new filoutputstream (file);         stringbuffer sb=new stringbuffer ();     //start writing XML content      sb.append ("<?xml version= ' 1.0 '  encoding= ' utf-8 '  standalone= ' true '  ?> ');     sb.append ("<info>");     sb.append ("<student id=" "+id+" > "),     sb.append (" <name> "+name+" </name> ");     Sb.append ("<age>" +age+ "</age>");     sb.append ("</student>");     sb.append ("</info>");         fos.write (Sb.toString () . GetBytes ());     fos.close;//Close Stream    }catch (exception e) {        e.printstacktrace ();    }

2. Write through the XML class in Android Util

String Name=.gettext (). ToString () string Age=.gettext (). ToString () string Id=.gettext (). ToString ()

XmlSerializer Xmlserializer=xml.newserializer ();

try{

FileOutputStream fos=new FileOutputStream (New File (Environment.getextrernalstoragedirectory, "info2.xml"));

Xmlserializer.setoutput (FOS, "utf-8");

Xmlserializer.setstartdocument ("Utf-8", true);

Xmlserializer.setstarttag (NULL, "info");

Xmlserializer.setstarttag (NULL, "student");

Xmlserializer.attribute (NULL, "ID", id);

Xmlserializer.setstarttag (NULL, "name");

Xmlserializer.text (name);

Xmlserializer.setendtag (NULL, "name");

Xmlserializer.setstarttag (NULL, "age");

Xmlserializer.text (age);

Xmlserializer.setendtag (NULL, "age");

Xmlserializer.setendtag (NULL, "student");

Xmlserializer.setendtag (NULL, "info");

Xmlserializer.setenddocument ();


Fos.close (); Close the stream

}catch (Exception e) {

E.printstacktrace ();

}



This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1937304

Generate an XML file in Android

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.