Document directory
- What is serialization?
- Summary
Note: This article from the http://www.moandroid.com.
In the first two articles, Android reads and writes XML (I) -- package instructions, Android reads and writes XML (medium) -- In sax, you must be familiar with the methods for reading XML files, here we will not talk much about it. We will explain how to write the information into an XML file. First, we will introduce the related classes in the andoid SDK.
| Class |
Description |
| Xmlserializer |
Define an interface to serialziation of XML infoset. defines an interface to serialize XML Information. |
What is serialization?
The stringized object also has a sequence called an object. Instead of simply saving the object in the memory, it allows us to transmit objects in the stream, objects can be transferred as basic data.
Next, let's look at a specific example to illustrate how to use xmlserializer.
Private string writexml () {<br/> xmlserializer serializer = xml. newserializer (); <br/> stringwriter writer = new stringwriter (); <br/> try {<br/> serializer. setoutput (writer); <br/> // <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Br/> serializer. startdocument ("UTF-8", true); <br/> // <blog number = "1"> <br/> serializer. starttag ("", "blog"); <br/> serializer. attribute ("", "Number", String. valueof (1); <br/> // <message data = "2009-09-23"> <br/> serializer. starttag ("", "message"); <br/> serializer. attribute ("", "date", ""); <br/> // <title> Android XML </title> <br/> serializer. starttag ("", "title"); <br/> serializer. text ("android XML "); <Br/> serializer. endtag ("", "title"); <br/>/< URL> http://www.moandroid.com /? P = 508 </URL> <br/> serializer. starttag ("", "url"); <br/> serializer. Text ("http://www.moandroid.com /? P = 508 "); <br/> serializer. endtag ("", "URL"); <br/>/</message> <br/> serializer. endtag ("", "message"); <br/> // </blog> <br/> serializer. endtag ("", "blog"); <br/> serializer. enddocument (); <br/> return writer. tostring (); <br/>}< br/> catch (exception e) <br/>{< br/> throw new runtimeexception (E ); <br/>}< br/>}
We will first generateString outputTo check whether the debugging information is output using log. V ("XML", writexml () as we designed. Run the program and view the debugging information output by the program in ddms. For details about how to view the debugging information, see How to Use Android ddms?
The output string is as follows:
<? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Br/> <blog number = "1"> <br/> <message data = "2009-09-23"> <br/> <title> Android XML </title> <br/> <URL> http://www.moandroid.com /? P = 508 </URL> <br/> </message> <br/> </blog>
After reading the running results, we can understand the functions of the code above. I will not explain them here.
NextWrite a string to a fileThe Code is as follows:
Public Boolean write (string path, string txt) <br/>{< br/> try <br/>{< br/> outputstream OS = openfileoutput (path, mode_private ); <br/> outputstreamwriter OSW = new outputstreamwriter (OS); <br/> OSW. write (txt); <br/> OSW. close (); <br/> OS. close (); <br/>}< br/> catch (filenotfoundexception e) <br/>{< br/> return false; <br/>}< br/> catch (ioexception e) <br/>{< br/> return false; <br/>}< br/> return true; <br/>}< br/>}
Add the following code to oncreate (): Write ("blog. xml", writexml ()). Run the program and open ddms to view the written XML document. The file is stored in the data/{package}/Files folder, as shown below:
You can also export this file from file explorer to your computer to view the specific content of the XML document, which is exactly the same as the output string.
Summary
The writing of XML files is complete. You must have some understanding about serialization. For details, refer to the packages in the android SDK.