Simple layout
< textview android:id = "@+id/tv_weather" Android : Layout_width = "Wrap_content" Android: Layout_height = "Wrap_content" Android: Layout_centerhorizontal = "true" = "true"
information class
Private int Wendu; Private int Wind ; Private String type; Private int ID; @Override Public String toString () { return "weather information [temperature =" + Wendu + ", wind =" + Winds + "level, weather condition =" + type
+ ", future =" + ID + "Day"; }
Four variables, then set and get respectively. The ToString function shows each of these four variables.
Parsing XML functions
Public classXmlReader { Public StaticList<xmlinfo> getXml (InputStream is)throwsException {//get the XML parserXmlpullparser parser =Xml.newpullparser (); //Initialize ParserParser.setinput (IS, "Utf-8"); //Parsing XML intEventType = Parser.geteventtype ();//the first time the call is positioned at the beginning of the XMLList<xmlinfo> Xmlinfos =NULL; Xmlinfo Xmlinfo=NULL; while(EventType! = xmlpullparser.end_document) {//need to keep the parser parsing the next bar Switch(eventtype) { CaseXmlpullparser.start_tag:if("Weather". Equals (Parser.getname ())) { //Discovery Start node creates a collection for weatherXmlinfos =NewArraylist<xmlinfo>(); } Else if("Day". Equals (Parser.getname ())) { //find a new date that corresponds to the weatherXmlinfo =NewXmlinfo (); String ID= Parser.getattributevalue (0); Xmlinfo.setid (Integer.parseint (id)); } Else if("Wendu". Equals (Parser.getname ())) {String Wendu=Parser.nexttext (); Xmlinfo.setwendu (Integer.parseint (Wendu)); } Else if("Wind". Equals (Parser.getname ())) {String Wind=Parser.nexttext (); Xmlinfo.setwind (Integer.parseint (wind)); } Else if("Type". Equals (Parser.getname ())) {String type=Parser.nexttext (); Xmlinfo.settype (type); } Break; CaseXmlpullparser.end_tag:if("Day". Equals (Parser.getname ())) {Xmlinfos.add (xmlinfo); } Break; default: Break; } EventType= Parser.next ();//The control parser resolves the next node} is.close (); returnXmlinfos; }}
Processing in OnCreate
TextView TV =(TextView) Findviewbyid (R.id.tv_weather); StringBuilder SB=NewStringBuilder (); List<XmlInfo>Xmlinfos; Try{Xmlinfos= Xmlreader.getxml (getClassLoader (). getResourceAsStream ("Weather.xml")); for(Xmlinfo Xmlinfo:xmlinfos) {sb.append (xmlinfo.tostring ()); Sb.append ("\ n"); } tv.settext (Sb.tostring ()); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); Toast.maketext ( This, "Parsing information failed", 0). Show (); }
I'm the dividing line of the king of the Land Tiger.
Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01
XML parsing. zip
Reprint Please specify source: Http://www.cnblogs.com/yydcdut