Android generates an XML file

Source: Internet
Author: User
<span id="Label3"></p><p><p>Generate an XML file backup SMS in the form Of:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><?</span></span><span style="color: #ff00ff;"><span style="color: #ff00ff;">XML version= "1.0" encoding= "UTF-8" standalone= "true"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">?><br></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">message</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"> <br><</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>NO. 0 Message<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1465041254178<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>000<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"> <br><</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1th message<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1465041254179<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>111<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"> <br><</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>2nd message<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Body</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1465041254179<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Date</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>222<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Address</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span>1<span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">type</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">SMS</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">message</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p><span style="line-height: 1.5;">Set up SMS Classes</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.wuyudong.createxml.domain;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Sms {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String body; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String date; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String type; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String address; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Sms (string body, string date, string type, string Address) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. BODY =<span style="color: #000000;"><span style="color: #000000;">body; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Date =<span style="color: #000000;"><span style="color: #000000;">date; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Type =<span style="color: #000000;"><span style="color: #000000;">type; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Address =<span style="color: #000000;"><span style="color: #000000;">address; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getbody () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">body; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setbody (String Body) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. BODY =<span style="color: #000000;"><span style="color: #000000;">body; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getDate () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">date; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setDate (String Date) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Date =<span style="color: #000000;"><span style="color: #000000;">date; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getType () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">type; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">SetType (String Type) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Type =<span style="color: #000000;"><span style="color: #000000;">type; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getaddress () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">address; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setaddress (String Address) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Address =<span style="color: #000000;"><span style="color: #000000;">address; }}</span></span></pre><p><p>the <span style="line-height: 1.5;">whole number of virtual SMS objects, exist in list,</span> <span style="line-height: 1.5;">backup data is usually backed up to an SD card</span></p></p><p><p>Using StringBuffer stitching strings, * Append all nodes of the entire XML file into SB objects</p></p><p><p>Sb.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' Yes '?> ');<br>To add a SMSS start node<br>Sb.append ("<smss>");<br>.......<br>* Write SB into the output stream</p></p><p><p>Fos.write (sb.tostring (). getBytes ());</p></p><p><p>The complete code is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">com.wuyudong.createxml;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.File;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.FileOutputStream;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.ArrayList;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.util.List;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">com.wuyudong.createxml.domain.Sms;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Environment;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.Menu;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {List</span></span><Sms><span style="color: #000000;"><span style="color: #000000;">message; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create 10 Virtual SMS</span></span>Message =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Arraylist<sms><span style="color: #000000;"><span style="color: #000000;">(); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < 10; i++<span style="color: #000000;"><span style="color: #000000;">) {sms SMS</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>SMS ("first" + i + "sms", system.currenttimemillis () + ""<span style="color: #000000;"><span style="color: #000000;">, </span></span>"1", "" + i + i +<span style="color: #000000;"><span style="color: #000000;">i); Message.add (sms); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Click (View v) {file file</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">File (environment.getexternalstoragedirectory (),</span></span>"backup.xml"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{fileoutputstream Fos</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">FileOutputStream (file); StringBuffer SB</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuffer (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add an XML header</span></span>Sb.append ("<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' Yes '?>"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add root node</span></span>Sb.append ("<message>"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add an SMS node for each SMS message</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span><span style="color: #000000;"><span style="color: #000000;">(Sms Sms:message) {sb.append (</span></span>"<sms>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"<body>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (sms.getbody ()); Sb.append (</span></span>"</body>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"<date>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (sms.getdate ()); Sb.append (</span></span>"</date>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"<address>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (sms.getaddress ()); Sb.append (</span></span>"</address>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"<type>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (sms.gettype ()); Sb.append (</span></span>"</type>"<span style="color: #000000;"><span style="color: #000000;">); Sb.append (</span></span>"</sms>"<span style="color: #000000;"><span style="color: #000000;">); } sb.append (</span></span>"</message>"<span style="color: #000000;"><span style="color: #000000;">); Fos.write (sb.tostring (). getBytes ()); Fos.close (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } }}</span></span></pre><p><p>Android generates an XML file</p></p></span>

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.