Android-Parsing XML

Source: Internet
Author: User

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

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.