InputStream Stream parsing xml file

Source: Internet
Author: User

1     //parsing XML files based on InputStream stream2     Private voidGetnewsfromstream (InputStream is) {3         //1. Creating an XML Pull parser Google built-in parser4Xmlpullparser XP =Xml.newpullparser ();5         Try {6             //2. Specify the encoding used by the parser to parse the file and parse the file7Xp.setinput (IS, "Utf-8");8             //3. Start parsing the file (JavaBean required before parsing, create JavaBean first)9             Ten             //Gets the event type, using the event type to determine what node is currently resolved One             intType =Xp.geteventtype (); AArraylist<news> newslist=NULL; -News news=NULL; -              while(type!=xmlpullparser.end_document) { the                 //different operations by resolving different nodes -                 Switch(type) { -                  CaseXmlpullparser.start_tag: -                     //the getname of the pull parser is the name of the current node.  +                     if("Newslist". Equals (Xp.getname ())) { -Newslist =NewArraylist<news>(); +                     } A                     Else if("News". Equals (Xp.getname ())) { atNews =NewNews (); -                     } -                     Else if("title". Equals (Xp.getname ())) { -                         Try { -String title =Xp.nexttext (); - News.settitle (title); in}Catch(IOException e) { -                             //TODO auto-generated Catch block to e.printstacktrace (); +                         } -                     } the                     Else if("comment". Equals (Xp.getname ())) { *                         Try { $String comment =Xp.nexttext ();Panax Notoginseng news.setcomment (comment); -}Catch(IOException e) { the                             //TODO auto-generated Catch block + e.printstacktrace (); A                         } the                     } +                     Else if("Detail". Equals (Xp.getname ())) { -                         Try { $String detail =Xp.nexttext (); $ news.setdetail (detail); -}Catch(IOException e) { -                             //TODO auto-generated Catch block the e.printstacktrace (); -                         }Wuyi                     } the                     Else if("Image". Equals (Xp.getname ())) { -                         Try { WuString ImageUrl =Xp.nexttext (); - News.setimageurl (ImageUrl); About}Catch(IOException e) { $                             //TODO auto-generated Catch block - e.printstacktrace (); -                         } -                     } A                      Break; +                  CaseXmlpullparser.end_tag: the                     if("News". Equals (Xp.getname ())) { - Newslist.add (news); $                     } the                      Break; the                 } the                 Try { the                     //Let the parser's pointer move back and return the event type -Type =Xp.next (); in}Catch(IOException e) { the                     //TODO auto-generated Catch block the e.printstacktrace (); About                 } the             } the              the}Catch(xmlpullparserexception e) { +             //TODO auto-generated Catch block - e.printstacktrace (); the         }Bayi     } the  the}

InputStream Stream parsing xml file

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.