Android creates an XML file.

Source: Internet
Author: User
  1. * Generate an XML string */
  2. Public String writexmlstr ()
  3. {
  4. Xmlserializer serializer = xml. newserializer ();
  5. Stringwriter writer = new stringwriter ();
  6. Try
  7. {
  8. Serializer. setoutput (writer );
  9. Serializer. startdocument ("UTF-8", true );
  10. Serializer. starttag ("", "project ");
  11. Serializer. Attribute ("", "date", "2011-01-15 ");
  12. Serializer. starttag ("", "graphics ");
  13. Serializer. Comment ("*********** comment ***********");
  14. Serializer. starttag ("", "graphics1 ");
  15. Serializer. starttag ("", "name ");
  16. Serializer. Text ("logo name ");
  17. Serializer. endtag ("", "name ");
  18. Serializer. cdsect ("CDATA data ");
  19. Serializer. endtag ("", "graphics1 ");
  20. Serializer. starttag ("", "graphics2 ");
  21. Serializer. starttag ("", "name ");
  22. Serializer. Text ("logo name ");
  23. Serializer. endtag ("", "name ");
  24. Serializer. cdsect ("CDATA data ");
  25. Serializer. endtag ("", "graphics2 ");
  26. Serializer. endtag ("", "graphics ");
  27. Serializer. endtag ("", "project ");
  28. Serializer. enddocument ();
  29. Return writer. tostring ();
  30. }
  31. Catch (exception E)
  32. {
  33. Throw new runtimeexception (E );
  34. }
  35. }
  36. /* Write the string to a private folder and store the file in data/package/Files */
  37. Public void writefiledata (string filename, string message)
  38. {
  39. Try
  40. {
  41. Fileoutputstream fout = openfileoutput (filename, Android. content. Context. mode_private );
  42. Byte [] bytes = message. getbytes ();
  43. Fout. Write (bytes );
  44. Fout. Close ();
  45. }
  46. Catch (exception e ){
  47. E. printstacktrace ();
  48. }
  49. }
  50. /* Call format writefiledata ("xmltest. xml", writexmlstr ())*/

/* Generate an XML string */<br/> Public String writexmlstr () <br/>{< br/> xmlserializer serializer = xml. newserializer (); <br/> stringwriter writer = new stringwriter (); <br/> try <br/>{< br/> serializer. setoutput (writer); <br/> serializer. startdocument ("UTF-8", true); <br/> serializer. starttag ("", "project"); <br/> serializer. attribute ("", "date", "2011-01-15"); </P> <p> serializer. starttag ("", "graphics"); <br/> serializer. comment ("********************"); </P> <p> serializer. starttag ("", "graphics1"); <br/> serializer. starttag ("", "name"); <br/> serializer. text ("logo name"); <br/> serializer. endtag ("", "name"); <br/> serializer. cdsect ("CDATA data"); <br/> serializer. endtag ("", "graphics1"); </P> <p> serializer. starttag ("", "graphics2"); <br/> serializer. starttag ("", "name"); <br/> serializer. text ("logo name"); <br/> serializer. endtag ("", "name"); <br/> serializer. cdsect ("CDATA data"); <br/> serializer. endtag ("", "graphics2"); </P> <p> serializer. endtag ("", "graphics"); <br/> serializer. endtag ("", "project"); </P> <p> serializer. enddocument (); <br/> return writer. tostring (); <br/>}< br/> catch (exception e) <br/>{< br/> throw new runtimeexception (E ); <br/>}</P> <p>/* write the string to a private folder and store the file in data/package/Files */<br/> Public void writefiledata (string filename, string message) <br/>{< br/> try <br/>{< br/> fileoutputstream fout = openfileoutput (filename, android. content. context. mode_private); <br/> byte [] bytes = message. getbytes (); <br/> fout. write (bytes); <br/> fout. close (); <br/>}< br/> catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p>/* Call format writefiledata ("xmltest. XML ", writexmlstr ())*/

 

Output file

[XHTML]
View plaincopyprint?
  1. <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
  2. -<Project date = "2011-01-15">
  3. -<Graphics>
  4. -<! -- ********************* -->
  5. -<Graphics1>
  6. <Name> logo name </Name>
  7. -<! [CDATA [CDATA]>
  8. </Graphics1>
  9. -<Graphics2>
  10. <Name> logo name </Name>
  11. -<! [CDATA [CDATA]>
  12. </Graphics2>
  13. </Graphics>
  14. </Project>

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.