Android Pull parsing related understanding

Source: Internet
Author: User

Pull parsing xml
The Xmlpullparser parser works like a SAX parser. It provides similar events (start and end elements), but needs to use the Parser.next () method to extract them. The event is sent as a numeric code, Therefore, different event code values can be processed differently. Gets the code value of the event by using the Parser.geteventtype () method (for example, Xmlpullparser.start_document, Xmlpullparser.start_ TAG, Xmlpullparser.end_tag).
When you are in an element, you can call Xmlpullparser's Getattributte () method to get the value of the property, or you can call its nexttext () to get the value of this node.
Specific ideas:
* Build Pullxmlparser parser
Xmlpullparser parser = Xml.newpullparser ();
* The XML documentregistering in the parser
Parser.setinput ();
* Trigger Event,Get event Type code
int event = Parser.geteventtype ();
*specific analysis for events
"Name". Equals (Parser.getname ());//Determines whether the start tag element is name
Parser.getattributevalue (0);//Get the property value of the tag
Parser.nexttext ();//Gets the value of the next text node
* Trigger and enter the next event
event = Parser.next ();
Pull parsing and sax parsing are not the same places:
(1) Pull reads the XML file and triggers the corresponding event call method to return a number
(2) Pull can be in the program control where you want to resolve to stop parsing.
Pros: Not a single load, can stop halfway

Pull parsing code *************

1  Public classPullparseractivityextendsActivity {2 PrivateTextView Mtextview;3 PrivateListView Mlistview;4 5 @Override6 protected voidonCreate (Bundle savedinstancestate) {7 8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.domparser);TenMtextview =(TextView) Findviewbyid (r.id.textview1); OneMlistview =(ListView) Findviewbyid (r.id.listview1); AMtextview.settext ("Pull parse xml");  -  -list<student> list =parser (); thearrayadapter<student> adapter =NewArrayadapter<student> ( This,  - Android. R.layout.simple_list_item_1, list);  - Mlistview.setadapter (adapter); -  + }  -  + PrivateList<student>parser () { Alist<student> list =NewArraylist<student>();  at //Get Pull Parser -Xmlpullparser parser =Xml.newpullparser (); - Try {  - //loading an XML document -Parser.setinput (pullparseractivity. This. getClassLoader () -. getResourceAsStream ("Student.xml"), "Utf-8");  in //C. Trigger event, Get event type code - intevent =Parser.geteventtype (); toStudent Student =NULL;  +  -  while(Event! =xmlpullparser.end_document) {  the Switch(event) { *  CaseXmlpullparser.start_document://Document Start $  Break; Panax Notoginseng  CaseXmlpullparser.start_tag://start Tag - if("Student". Equals (Parser.getname ())) {  theStudent =NewStudent (); + Student.setid (integer.valueof (parser A. Getattributevalue (0)));  the  + }  -LOG.I ("TAG", "infor");  $ if(Student! =NULL) {  $ if("Name". Equals (Parser.getname ())) {  - Student.setname (Parser.nexttext ()); -}Else if("Age". Equals (Parser.getname ())) {  the Student.setage (integer.valueof (parser -. Getattributevalue (0))); Wuyi }  the }  -  Break;  Wu  CaseXmlpullparser.end_tag://End Bundle Label - if("Student". Equals (Parser.getname ())) {  About List.add (student); $Student =NULL;  - }  -LOG.I ("TAG", "infor");  -  Break;  A  CaseXmlpullparser.text://Text parsing +  Break;  the }  -event =Parser.next (); $  the }  the}Catch(Exception e) { the e.printstacktrace (); the }  -  in returnlist; the }  the}

Android Pull parsing related understanding

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.