Dynamic XML Parsing

Source: Internet
Author: User

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 ;}

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.