<?XML version= "1.0" encoding= "Utf-8"?><Resources> <Users> <Username= "Jason" Age= " a" Location= "Beijing"/> <Username= "Peter" Age= "+" Location= "Shanghai"/> </Users></Resources>
/*** (Get the properties of a third-party module from an XML configuration file) *@paramfragmentactivity Activity *@paramxmlid The ID of the XML file *@returnlist<user> *@throwsxmlpullparserexception *@throwsIOException*/ Public StaticList<user> Getusersfromxml (fragmentactivity fragmentactivity,intxmlid)throwsxmlpullparserexception, IOException {xmlresourceparser xmlparser=fragmentactivity.getresources (). GETXML (xmlid); List<User> users =NewArraylist<user>(); intEventType =Xmlparser.geteventtype (); //continue parsing, not the end of the file while(EventType! =xmlresourceparser.end_document) { Switch(eventtype) {//start tag for the contents of the file, note that the starting tag here is the Thirdpartyusers.xml file//inside <ThirdPartyUsers> Tag the first label below Thirdpartyuser CaseXmlResourceParser.START_TAG:String TagName=Xmlparser.getname (); if(Tagname.endswith ("Thirdpartyuser") ) {User User=NewUser (); User.user= Xmlparser.getattributevalue (NULL, "name"); User.age= Xmlparser.getattributevalue (NULL, "Age"); User.location= Xmlparser.getattributevalue (NULL, "Location"); Users.add (user); } Break; CaseXmlresourceparser.end_tag: Break; CaseXmlresourceparser.text: Break; default: Break; } EventType=Xmlparser.next (); } xmlparser.close (); returnusers; }
Android Custom XML parsing