Android uses pull to parse XML data

Source: Internet
Author: User

The data is as follows

<videonews><C:foreachItems= "${videos}"var= "Video" >    <NewsID= "${video.getid ()}" >         <title>${video.getname ()}</title>         < Time>${video.gettime ()}</ Time>     </News></C:foreach></videonews>

Get InputStream

String path= "Http://222.133.40.188:8080/androidServer/test"; URL URL=NewURL (path); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout (5000); Connection.setrequestmethod ("GET"); if(Connection.getresponsecode () ==200) {InputStream instream=Connection.getinputstream (); //get the file stream, convert the file to XML            returnParsexml (instream); }Else{            return NULL; }

Parse inputstream--"xml

PrivateList<news> Parsexml (InputStream instream)throwsXmlpullparserexception, IOException {News news=NULL; List<News> newslist=NewArraylist<news>(); Xmlpullparser Pull=Xml.newpullparser (); Pull.setinput (instream,"Utf-8"); intEvent=Pull.geteventtype ();  while(event!=xmlpullparser.end_document) {            Switch(event) { CaseXmlpullparser.start_tag:if("News". Equals (Pull.getname ())) {                    intId=integer.parseint (Pull.getattributevalue (0)); News=NewNews ();                News.setid (ID); }Else if("title". Equals (Pull.getname ())) {String name=Pull.gettext ();                News.setname (name); }Else if("Time". Equals (Pull.getname ())) {String time=Pull.gettext ();                News.settime (time); }                 Break;  CaseXmlpullparser.end_tag:if("News". Equals (Pull.getname ()))                {Newslist.add (news); }             Break; } Event=Pull.next (); }        returnnewslist; }

Android uses pull to parse XML data

Related Article

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.