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