Android parses XML strings

Source: Internet
Author: User
Package. test; import Java. io. bytearrayinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. stringreader; import javax. XML. parsers. saxparser; import javax. XML. parsers. saxparserfactory; import Org. XML. sax. inputsource; import Org. XML. sax. xmlreader; import Org. xmlpull. v1.xmlpullparser; import Org. xmlpull. v1.xmlpullparserexception; import biz. source_code.base64coder.base64coder; imp Ort. test. examplehandler; import. test. parsedexampledataset; import android. app. activity; import android. OS. bundle; import android. util. XML; import android. widget. edittext; import android. widget. textview; public class B extends activity {/** called when the activity is first created. */edittext TV1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcon Tentview (R. layout. main); TV1 = (edittext) findviewbyid (R. id. TV); tv1.settext ("000"); string xml = "<vcom version = '1. 1 '> <loginlink> 11111023 </loginlink> <errmsg> dfft </errmsg> </vcom> "; tv1.settext (XML); bytearrayinputstream tinputstringstream = NULL; try {If (XML! = NULL &&! XML. trim (). equals ("") {tinputstringstream = new bytearrayinputstream (XML. getbytes () ;}} catch (exception e) {// todo: handle exception tv1.settext (E. getmessage (); return;} xmlpullparser parser = xml. newpullparser (); try {parser. setinput (tinputstringstream, "UTF-8"); int eventtype = parser. geteventtype (); While (eventtype! = Xmlpullparser. end_document) {Switch (eventtype) {Case xmlpullparser. start_document: // document start event. Data initialization can be performed. // persons = new arraylist <person> (); break; Case xmlpullparser. start_tag: // start element event string name = parser. getname (); If (name. inclusignorecase ("errmsg") {// currentperson = new person (); // currentperson. setid (new // INTEGER (parser. getattributevalue (null, "ID"); tv1.settext (parser. nexttex T ();} // else if (currentperson! = NULL) {// If (name. equalsignorecase ("loginlink") {// currentperson. setname (parser. nexttext (); // if it is followed by a text node, it returns its value //} else if (name. inclusignorecase ("errmsg") {// currentperson. setage (new short (parser. nexttext (); //} break; Case xmlpullparser. end_tag: // End Element event // If (parser. getname (). equalsignorecase ("person") // & currentperson! = NULL) {// persons. add (currentperson); // currentperson = NULL; //} break;} eventtype = parser. next ();} tinputstringstream. close (); // return persons;} catch (xmlpullparserexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}
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.