Normally, there is three type parser in Android. Xmlpullparser, DOM & SAX.
Google Recomand Xmlpullparser to doing.
parser XML files or InputStream, it based on XML content.
So using a general-to-parser XML.
Basexmlobj:
/*********************************************************** * @description: This class function is TODO * * @create AUT Hor:deman.lu * @create date:2015-07-15 * @modify Author: * @modify Date: * @contact: [email protected] * ******** **************************************************/ PackageCom.joyfulmath.androidstudy.connect.xmlparser;ImportJava.lang.reflect.Field; Public Abstract classBasexmlobj { Public Abstractstring[] Getnodes (); Public voidSetparamater (String tag, Object value) {Try{Field field=getclass (). GetField (tag); Field.setaccessible (true); Field.set ( This, value); } Catch(SecurityException e) {e.printstacktrace (); } Catch(nosuchfieldexception e) {e.printstacktrace (); } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } }}
/*********************************************************** * @description: This class function is TODO * * @create AUT Hor:deman.lu * @create date:2015-07-15 * @modify Author: * @modify Date: * @contact: [email protected] * ******** **************************************************/ PackageCom.joyfulmath.androidstudy.connect.xmlparser;ImportJava.io.InputStream;ImportOrg.xmlpull.v1.XmlPullParser;Importorg.xmlpull.v1.XmlPullParserException;Importorg.xmlpull.v1.XmlPullParserFactory;ImportAndroid.content.res.XmlResourceParser; Public classXmlparserutils {Private StaticString encode = "Utf-8"; Public StaticXmlpullparser Pullparser; Static { Try{Pullparser=xmlpullparserfactory.newinstance (). Newpullparser (); } Catch(xmlpullparserexception e) {e.printstacktrace (); } } /*** * @description: Parses the contents of a node and encapsulates it into an object model. * * @author: Deman.lu * * @create: 2015-07-15 * *@paramIn network data contains XML * *@paramobj * *@throwsException * *@return: T extends Basexmlobj*/ Public Static<textendsBasexmlobj>voidStreamtext2model (InputStream in, T obj)throwsException {pullparser.setinput (in, encode); intEventType =Pullparser.geteventtype (); String[] Nodes=obj.getnodes (); String NodeName=NULL; BooleanSuccess =true; while(EventType! = xmlpullparser.end_document &&success) { Switch(eventtype) { Casexmlpullparser.start_document: Break; CaseXmlPullParser.START_TAG:nodeName=Pullparser.getname (); Break; CaseXmlpullparser.text: for(inti = 0; i < nodes.length; i++) { if(Nodes[i].equals (nodeName)) {Obj.setparamater (NodeName, Pullparser.gettext ()); } } Break; CaseXmlpullparser.end_tag: Break; } EventType=Pullparser.next (); } } /*** * @description: Parses the contents of a node and encapsulates it into an object model. * * @author: Deman.lu * * @create: 2015-07-15 * *@paramresparser xmlresourceparser Read form resource * *@paramobj * *@throwsException * *@return: void*/ Public Static<textendsBasexmlobj>voidStreamtext2model (xmlresourceparser resparser, T obj)throwsException {intEventType =Resparser.geteventtype (); String[] Nodes=obj.getnodes (); String NodeName=NULL; BooleanSuccess =true; while(EventType! = xmlpullparser.end_document &&success) { Switch(eventtype) { Casexmlpullparser.start_document: Break; CaseXmlPullParser.START_TAG:nodeName=Resparser.getname (); Break; CaseXmlpullparser.text: for(inti = 0; i < nodes.length; i++) { if(Nodes[i].equals (nodeName)) {Obj.setparamater (NodeName, Resparser.gettext ()); } } Break; CaseXmlpullparser.end_tag: Break; } EventType=Resparser.next (); } } }
We using reflect, but if XML is complex, the efficiency could be low.
The should be decided.
PackageCom.joyfulmath.androidstudy.connect;Importjava.io.Serializable;ImportCOM.JOYFULMATH.ANDROIDSTUDY.CONNECT.XMLPARSER.BASEXMLOBJ; Public classStudentextendsBasexmlobjImplementsSerializable {/** * */ Private Static Final LongSerialversionuid = 1L; PublicString Gradeid, Gradename, ClassID, ClassName, UserID, UserName; PublicStudent () {} @Override Publicstring[] Getnodes () {return NewString[] {"Gradeid", "Gradename", "ClassID", "ClassName", "UserID", "UserName" }; } @Override Publicstring toString () {string Su= "@student:" + "Gradeid:" +Gradeid+ "Gradename:" +Gradename+ "ClassID:" +ClassID+ "ClassName:" +ClassName+ "UserID:" +UserID+ "UserName:" +UserName; returnNetworkutils.replaceblank (SU); } }
PackageCom.joyfulmath.androidstudy.connect;Importcom.joyfulmath.androidstudy.connect.xmlparser.XmlParserUtils;ImportAndroid.content.Context;ImportAndroid.content.res.XmlResourceParser; Public classXmlparserdemo { Public StaticStudent Parserstudentxml (Context context,intresId) { Try{Student Student=NewStudent (); Xmlresourceparser XML=context.getresources (). GETXML (ResId); Xmlparserutils.streamtext2model (XML, student); Xml.close (); returnstudent; } Catch(Exception e) {e.printstacktrace (); } return NULL; }}
<?xml version= "1.0" encoding= "UTF-8"?><dsxml> <IsLog>true </IsLog> <gradeid>10001 </GradeID> <GradeName> freshman </GradeName> < classid>10010 </ClassID> <ClassName> (</classname) class > <userid>10000 </UserID> <UserName> Zhang San </UserName> </DsXml>
Reprint: http://www.cnblogs.com/vanezkw/archive/2013/03/03/2941496.html
Android Network Develop (3)----XML Parser