Xmlpullparser (Android recommended)

Source: Internet
Author: User

Add an XML file Xmlpullparser

 PackageCom.it.heima.review;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportOrg.xmlpull.v1.XmlSerializer;ImportAndroid. R.xml;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.text.TextUtils;Importandroid.util.Xml;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Ed_ssname; PrivateRadiogroup RGB; PrivateButton Btnbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Btnbutton=(Button) Findviewbyid (R.id.btnsave); Ed_ssname=(EditText) Findviewbyid (r.id.ed_name); RGB=(Radiogroup) Findviewbyid (R.ID.RGB); Btnbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubString Studentname =Ed_ssname.gettext (). toString (). Trim ();                String sex; if(Textutils.isempty (studentname)) {Toast.maketext (Getapplicationcontext (),"SSSs", 0); } Else {                    intID =Rgb.getcheckedradiobuttonid (); if(id = =r.id.male) {sex= "Male"; } Else{Sex= "female"; } File File=NewFile (Getfilesdir (), Studentname + ". Xml"); Try{OutputStream out=Newfileoutputstream (file); XmlSerializer Serializer=Xml.newserializer (); Serializer.setoutput (out,"Utf-8"); Serializer.startdocument ("Utf-8",true); Serializer.starttag (NULL, "name");                        Serializer.text (Studentname); Serializer.endtag (NULL, "name"); Serializer.starttag (NULL, "Sex");                        Serializer.text (Sex); Serializer.endtag (NULL, "Sex");                        Serializer.enddocument (); Toast.maketext (Getapplicationcontext (),"Sucess", 0); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace ();    }                }            }        }); }}

Analytical

 PackageCom.itheima.pullparser;Importjava.io.IOException;ImportJava.io.InputStream;ImportOrg.xmlpull.v1.XmlPullParser;Importorg.xmlpull.v1.XmlPullParserException;Importcom.itheima.pullparser.domain.Product;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.util.Xml;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //using the XML pull parser to parse the contents of an XML file//XmlXmlpullparser Pullparser =Xml.newpullparser (); Try{InputStream in= Getassets (). Open ("Result.xml"); //What is the source of the parsing??? ---result.xmlPullparser.setinput (In, "GBK"); //get the type of an event            intEventType =Pullparser.geteventtype ();/*<?xml version= "1.0" encoding= "GBK"?><smartresult> <product type= "mobile" > <phonenum> 13512345678</phonenum> <location> Chongqing Mobile Shenzhou Line card </location> <phoneJx> gains, gaudy, must be anti-robbery money, the beginning to protect the auspicious belt fierce < /phonejx> </product></smartresult> **/Product P=NULL;  while(eventtype!=xmlpullparser.end_document) {                                if(eventtype==Xmlpullparser.start_tag) {                                        //determines whether the start of an element, as long as it is the beginning of an element, will enter here//gets the name of the element that is currently resolved to                    if("Product". Equals (Pullparser.getname ())) {P=NewProduct (); //Sax Parsing//prepare an instance of the product class to encapsulate the dataString type = pullparser.getattributevalue (0);                    P.settype (type); }Else if("Phonenum". Equals (Pullparser.getname ())) {                                                //Get 13512345678//<phonenum>13512345678</phonenum>String Phonenum =Pullparser.nexttext ();                    P.setphonenum (Phonenum); }Else if("Location". Equals (Pullparser.getname ())) {                                                //<location> Chongqing Mobile Shenzhou line card </location>String location =Pullparser.nexttext ();                    P.setlocation (location); }Else if("PHONEJX". Equals (Pullparser.getname ())) {                        //<phoneJx> gains, gaudy, must be anti-robbery money, the beginning of the Peace Ji belt fierce </phoneJx>String PHONEJX =Pullparser.nexttext ();                        Pullparser.gettext ();                    P.SETPHONEJX (PHONEJX); }                }                //manual Nudge "pointer"EventType =Pullparser.next (); }                        if(p!=NULL) {System.out.println (p.tostring ()); }                    } Catch(Exception e) {e.printstacktrace (); }                    }}

Xmlpullparser (Android recommended)

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.