Generating an XML file using an XML serializer and parsing an XML file with pull

Source: Internet
Author: User

First, specify the XML format, and the XML format I specify is as follows:

<?XML version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><message>    <SMS>        <Body>Airbnb 0</Body>        <Date>1462162910995</Date>        <Address>1380</Address>        <type>1</type>    </SMS><message>

I. Using an XML serializer to generate an XML file

//To generate an XML file using an XML serializer//1. Get the Serializer objectXmlSerializer xs =Xml.newserializer (); //2. InitializeFile File =NewFile (Environment.getexternalstoragedirectory (), "Sms2.xml"); Try{FileOutputStream fos=Newfileoutputstream (file); //encoding: Specifies what encoding to use to generate the XML fileXs.setoutput (FOS, "Utf-8"); //3. Start generating XML files//Encoding: Specifies the value of the encoding property in the head node//Standalone: Whether the XML file is independentXs.startdocument ("Utf-8",true); Xs.starttag (NULL, "message");//Start Node                         for(Message msg:list) {Xs.starttag (NULL, "SMS"); Xs.starttag (NULL, "Body");                Xs.text (Msg.getbody ()); Xs.endtag (NULL, "Body"); Xs.starttag (NULL, "Date");                Xs.text (Msg.getdate ()); Xs.endtag (NULL, "Date"); Xs.starttag (NULL, "Address");                Xs.text (Msg.getaddress ()); Xs.endtag (NULL, "Address"); Xs.starttag (NULL, "type");                Xs.text (Msg.gettype ()); Xs.endtag (NULL, "type"); Xs.endtag (NULL, "SMS"); } Xs.endtag (NULL, "message");//End Node//tell the serializer that the build is completexs.enddocument (); } Catch(Exception e) {e.printstacktrace (); }        

Ii. using pull to parse an XML file

File File =NewFile (Environment.getexternalstoragedirectory (), "Sms2.xml"); Try{FileInputStream fis=Newfileinputstream (file); //get the Pull parser objectXmlpullparser XP =Xml.newpullparser (); //InitializeXp.setinput (FIS, "Utf-8"); //gets the event type of the current node, through the event type, we can know what node the current node is,//so that we can determine what we should do.            intType =Xp.geteventtype (); Message msg=NULL;  while(Type! =xmlpullparser.end_document) {                //depending on the type of node, different operations are performed                Switch(type) { CaseXmlpullparser.start_tag://gets the name of the current node.                    if("Message". Equals (Xp.getname ())) {                        //Create a Message collection ObjectList =NewArraylist<message>(); }Else if("SMS". Equals (Xp.getname ())) {                        //Create a message JavaBeanmsg =NewMessage (); }Else if("Body". Equals (Xp.getname ())) {                        //gets the text of the next node of the current node (that is, Airbnb 0)Msg.setbody (Xp.nexttext ()); }Else if("Date". Equals (Xp.getname ())) {                        //gets the text of the next node of the current node (that is, Airbnb 0)msg.setdate (Xp.nexttext ()); }Else if("Address". Equals (Xp.getname ())) {                        //gets the text of the next node of the current node (that is, Airbnb 0)msg.setaddress (Xp.nexttext ()); }Else if("Type". Equals (Xp.getname ())) {                        //gets the text of the next node of the current node (that is, Airbnb 0)Msg.settype (Xp.nexttext ()); }                      Break;  CaseXmlpullparser.end_tag:if("SMS". Equals (Xp.getname ()))                    {List.add (msg); }                     Break; default:                     Break; }                                //move the pointer to the next node and return the event type for that nodeType =Xp.next (); }                         for(Message message:list) {System.out.println (message); }                    } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(xmlpullparserexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }

Generating an XML file using an XML serializer and parsing an XML file with pull

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.