For ease of use, dynamic parsing of XML has recently written a class for dynamic parsing of XML, on the code
/*** Parse XML dynamically * @ Param OBJ resolve the bean in the header section * @ Param. The Bean class for parsing the message content * @ Param xmlstring the XML passed in * @ return */Public static list tntresolvexml (Object OBJ, class linoleic, string xmlstring) {list resultlist = new arraylist (); // returns liststring isgetmessage = NULL; // indicates whether to start obtaining content. Try {object clabean = CIA. newinstance (); xmlpullparser parser = xml. newpullparser (); parser. setinput (New stringreader (xmlstring); I NT Event = parser. geteventtype (); While (event! = Xmlpullparser. end_document) {Switch (event) {Case xmlpullparser. start_tag: If (parser. getname (). equals ("OBJ") {isgetmessage = "Y" ;}// first parse the code returned by the header and other information if (isgetmessage = NULL) {field [] fields = obj. getclass (). getdeclaredfields (); // obtain the attribute method [] Methods = OBJ based on the class object. getclass (). getdeclaredmethods (); // obtain all methods for (field F: fields) {If (F. getname (). equals (parser. getname () {for (method me Thod: Methods) {string meth = method. getname (); // match the set method if (meth! = NULL & "set ". equals (meth. substring (0, 3) & modifier. ispublic (method. getmodifiers () & ("set" + character. touppercase (F. getname (). charat (0) + F. getname (). substring (1 )). equals (METH) {// call the Set Method and assign a value to the method. invoke (OBJ, parser. nexttext () ;}}}} if (isgetmessage! = NULL & isgetmessage. equals ("Y") {method methd = obj. getclass (). getmethod ("getcode"); methd. invoke (OBJ); If (methd. invoke (OBJ )! = NULL) {If (methd. invoke (OBJ ). equals ("100") {field [] cla_fields = clabean. getclass (). getdeclaredfields (); // obtain the attribute method [] cla_methods = clabean based on the class object. getclass (). getdeclaredmethods (); // obtain all methods based on the class object for (field cla_f: cla_fields) {If (cla_f.getname (). equals (parser. getname () {for (method cla_method: cla_methods) {string meth = cla_method.getname (); // match the set method if (meth! = NULL & "set ". equals (meth. substring (0, 3) & modifier. ispublic (cla_method.getmodifiers () & ("set" + character. touppercase (cla_f.getname (). charat (0) + cla_f.getname (). substring (1 )). equals (METH) {// call the Set Method and assign cla_method.invoke (clabean, parser. nexttext () ;}}}}} break; Case xmlpullparser. end_tag: If (parser. getname (). equals ("OBJ") & parser. getname ()! = NULL) {resultlist. add (clabean); clabean = linoleic. newinstance ();} default: break;} event = parser. next () ;}} catch (exception e) {e. printstacktrace ();} return resultlist ;}