Android XmlSerializer generate XML file

Source: Internet
Author: User

Example 1

The code is as follows Copy Code

try {

File F = new file (Getexternalcachedir (). GetAbsolutePath () + "My.xml");
OutputStream outPut = new FileOutputStream (f);

XmlSerializer Serializer=xml.newserializer ();

Serializer.setoutput (OutPut, "utf-8");

Serializer.startdocument ("Utf-8", true);

Serializer.starttag (NULL, "companys");

For (string[] s:taxicompany)

{

Serializer.starttag (null, dbutil.taxi_table);

Serializer.attribute (null, dbutil.key_province, s[0]);

Serializer.attribute (null, Dbutil.key_cityname, s[1]);

Serializer.attribute (null, Dbutil.key_name, s[2]);

Serializer.attribute (null, Dbutil.key_tele, s[3]);

Serializer.endtag (null, dbutil.taxi_table);

}

Serializer.endtag (NULL, "companys");

Serializer.enddocument ();

Output.close ();

catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Instance 2

The code is as follows Copy Code

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 to 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");
}
}

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.