Solve the problem of abnormal line change and uneven TextView of Android

Source: Internet
Author: User

Introduction to the pull parser

The pull parser operates in a similar way to the SAX parser. It provides similar events, such as starting elements and ending element events, using xmlpullparser. Next () to enter the next element and trigger the corresponding event. Unlike SAX, the pull parser produces an event that is a number, not a method, so you can use a switch to handle the event. When the element begins parsing, the Parser.nexttext () method is called to get the value of the next Text type node.

Pull parser source code and documentation download URL: http://www.xmlpull.org/

Parse Step 1. Create a Xmlpullparser parse object directly 2. Set the parse file input stream and specify how the input stream is encoded in the operation
3. Get the EventType time type returned when parsing a file
The 4.while loop iterates to the end of the document 5. Use Xmlpullparser.next () to enter the next element and trigger a 6.switch statement to loop through the results 7. At the end of the label, add to the collection 8. Freeing resourcesxml.newpullparser ()--->setinput-->geteventtype ()--->while (type!=xmlpullparser.end_document)-- Case Type---parser.getname () to judge----> get information to add to the object--->type = Parser.next (); The pointer moves down

Instance code: Resource File China.xml
<?xml version= "1.0" encoding= "UTF-8"? ><china dn= "Day" ><city cityname= "Jiangsu" Pyname= "Jiangsu"        Quname= "Jiangsu" state1= "1" state2= "1" statedetailed= "Cloudy" tem1= "tem2=" 19 "        Windstate= "Northwest Wind Level 3-4"/> <city cityname= "Beijing" Pyname= "Beijing" Quname= "Beijing" state1= "1"          state2= "1" statedetailed= "Cloudy" tem1= "tem2=" windstate= "Northwest Wind 5-6"/><city> <cityname> Henan </cityname> <pyName>henan</pyName> <quName> Henan </qunam e> <state1>1</state1> <state2>1</state2> <stateDetailed> Cloudy to Sunny &L t;/statedetailed> <tem1>38</tem1> <tem2>-1</tem2> <windState> East South Wind level 2-3 </windState> </city> </china>

Entity class City.java
Package Com.example.domain;public class City {private string cityname;private string Pyname;private string quname; private string State1;private string State2; @Overridepublic string toString () {return "City [cityname=" + CityName + ", py Name= "+ pyname+", quname= "+ Quname +", state1= "+ State1 +", state2= "+ State2 +", statedetailed= "+ statedetailed + ", tem1=" + Tem1 + ", tem2=" + tem2 + ", windstate=" + Windstate + "]";} private string Statedetailed;private string Tem1;private string tem2;private string Windstate;public string Getcityname ( ) {return cityname;} Public City () {super ();} public void Setcityname (String cityname) {this.cityname = CityName;} Public String Getpyname () {return pyname;} public void Setpyname (String pyname) {this.pyname = Pyname;} Public String Getquname () {return quname;} public void Setquname (String quname) {this.quname = Quname;} Public String getState1 () {return state1;} public void SetState1 (String state1) {this.state1 = state1;} Public String GetState2 () {return State2;} public void SetState2 (String state2) {this.state2 = State2;} Public String getstatedetailed () {return statedetailed;} public void setstatedetailed (String statedetailed) {this.statedetailed = statedetailed;} Public String getTem1 () {return tem1;} public void SetTem1 (String tem1) {this.tem1 = tem1;} Public String getTem2 () {return tem2;} public void setTem2 (String tem2) {this.tem2 = tem2;} Public String Getwindstate () {return windstate;} public void Setwindstate (String windstate) {this.windstate = windstate;}  Public City (String CityName, String pyname, String quname, String state1,string state2, String statedetailed, String tem1, String tem2,string windstate) {super (); this.cityname = Cityname;this.pyname = Pyname;this.quname = Quname;this.state1 = S Tate1;this.state2 = state2;this.statedetailed = Statedetailed;this.tem1 = tem1;this.tem2 = Tem2;this.windState = Windstate;}}

Parsing Tool class Pullxml.java
Package Com.example.util;import Java.util.arraylist;import Java.util.list;import org.xmlpull.v1.XmlPullParser; Import Android.util.xml;import Com.example.domain.city;public class Pullxml {public list<city> PullXml () {List <City> entities = null; City currentcity = null;//1. Create the Xmlpullparser parse object directly xmlpullparser Xmlpullparser = Xml.newpullparser (); try {//2. Sets the parse file input stream and specifies how the input stream is encoded in the Operation Xmlpullparser.setinput (GetClass (). getClassLoader (). getResourceAsStream ("China.xml"), " UTF-8 ");//3. Gets the EventType time type that is returned when parsing the file int eventtype = Xmlpullparser.geteventtype ();//While looping through the end of the document while (EventType! = xmlpullparser.end_document) {switch (eventtype) {case XmlPullParser.START_DOCUMENT:entities = new arraylist<city > (); break;case xmlpullparser.end_document:break;case XmlPullParser.START_TAG:String name = Xmlpullparser.getname () if (Name.equals ("City")) {//declares the current city object currentcity = new city (); int count = Xmlpullparser.getattributecount (); if ( Count > 0) {/* * cityname= "Beijing" Pyname= "Beijing" quname="Beijing" * state1= "1" state2= "1" statedetailed= "Cloudy" * tem1= "" tem2= "5-6" windstate= "Northwest" */currentcity.setcityname ( Xmlpullparser.getattributevalue (NULL, "CityName")), Currentcity.setpyname (Xmlpullparser.getattributevalue (NULL, " Pyname ")); Currentcity.setquname (Xmlpullparser.getattributevalue (null," Quname ")); Currentcity.setstate1 ( Xmlpullparser.getattributevalue (NULL, "State1")), Currentcity.setstate2 (Xmlpullparser.getattributevalue (NULL, " State2 ")); currentcity.setstatedetailed (Xmlpullparser.getattributevalue (null," statedetailed ")); CURRENTCITY.SETTEM1 (Xmlpullparser.getattributevalue (null, "TEM1")); CURRENTCITY.SETTEM2 ( Xmlpullparser.getattributevalue (NULL, "TEM2")), currentcity.setwindstate (Xmlpullparser.getattributevalue (NULL, " Windstate "));}} else if (currentcity! = null) {/* * <cityname> Henan </cityname> <pyName>henan</pyName> * <quname& gt; Henan </quName> <state1>1</state1> * <state2>1</state2> * <stateDetailed> Cloudy to clear </statedetailed> <tem1>38</tem1> * <tem2>-1</tem2> <windState> Southeast Wind level 2-3 </windState> */if ( Name.equalsignorecase ("CityName")) {Currentcity.setcityname (Xmlpullparser.nexttext ());} else if ( Name.equalsignorecase ("Pyname")) {Currentcity.setpyname (Xmlpullparser.nexttext ());} else if (name.equalsignorecase ("Quname")) {Currentcity.setquname (Xmlpullparser.nexttext ());} else if (name.equalsignorecase ("State1")) {currentcity.setstate1 (Xmlpullparser.nexttext ());} else if ( Name.equalsignorecase ("State2")) {Currentcity.setstate2 (Xmlpullparser.nexttext ());} else if (name.equalsignorecase ("statedetailed")) {currentcity.setstatedetailed (Xmlpullparser.nexttext ());} else if (name.equalsignorecase ("Tem1")) {currentcity.settem1 (Xmlpullparser.nexttext ());} else if ( Name.equalsignorecase ("tem2")) {currentcity.settem2 (Xmlpullparser.nexttext ());} else if (Name.equalsignorecase (" Windstate ")) {Currentcity.setwindstate (Xmlpullparser.nexttext ());}} Break;case XmlPullParser.END_TAG:String Names = xmlpullparser.getname ();//At the end of the label, add the IF (Xmlpullparser.getname (). Equalsignorecase ("City") to the collection ("City") && CurrentCity! = null) {//Add to the collection Entities.add (currentcity);//release resource currentcity = null;} Break;default:break;} Use Xmlpullparser.next () to enter the next element and trigger EventType = Xmlpullparser.next ();}} catch (Exception e) {e.printstacktrace ();} return entities;}}

Running results test the program results with the previous test method:

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.