Using reflection to construct VO from XML

Source: Internet
Author: User
Tags constructor reflection return access
Xml
Reflection enables your program code to access the internal information of classes loaded into the JVM, allowing you to write code that works in conjunction with execution, rather than the classes selected in the source code.        This makes reflection a major tool for building flexible applications. First look at the following XML file:

<?xml Version = ' 1.0 ' encoding = ' gb2312 '?><root> <arsubitem num= ' 1 ' > <c_itemno>30020050000 9</c_itemno> <c_itemname>20050112 Test </C_ITEMNAME> <c_subitemno>200543030000010</c_subi temno> <c_subitemname>20010112 Marking section 2</c_subitemname> <C_FUNCNAME> Supervision record </C_FUNCNAME> ;/arsubitem></root>

The XML file describes the current process information for a paragraph, and then translates it into VO:


public class Bidprocess implements serializable{

Private String ItemNo; Private String ItemName; Private String Subitemno; Private String Subitemname; Private String functionname;

 /**  * @return return to functionname.   */ public String getfunctionname () {  return functionname; } /**  * @param FunctionName to set the functionname.   */ public void Setfunctionname (String functionname) {  this.functionname = functionname; }  /**  * @return return to ItemName.   */ public String getitemname () {  return itemname; } /**  * @param itemname to set ItemName.   */ public void Setitemname (String itemname) {  this.itemname = itemname; } /**  * @return return to ItemNo.   */ public String Getitemno () {  return itemno; } /**  * @param itemno to set ItemNo.   */ public void Setitemno (String itemno) {  this.itemno = itemno; } /**  * @return Return to Subitemname.   */ public String getsubitemname () {  return subitemname; } /**  * @param Subitemname to set the Subitemname. &nbsP */ public void Setsubitemname (String subitemname) {  this.subitemname = subitemname; } /**   * @return return to Subitemno.   */ public String Getsubitemno () {  return subitemno; } /**  * @param subItemNo The Subitemno to set.   */ public void Setsubitemno (String subitemno) {  this.subitemno = subitemno; }}


From the XML construct VO, put it in the list with the following code:


public class Vofactory {

 //  constructed object Set  private ArrayList list=new ArrayList ()   /** using reflection to get result set   */ public List Parse (Serializable source, String XML) {  try{   //constructs an XML input stream     Bytearrayinputstream ba=new Bytearrayinputstream (Xml.getbytes ());    //dom4j initialization     Saxreader reader = new Saxreader ();    document Document = Reader.read (BA);    element root = Document.getrootelement ();    //get class object    class c = Source.getclass ();    //get constructor    constructor[] Cons=c.getdeclaredconstructors ();    Constructor con=cons[0];   //get the fields in the class    field[] fields = C.getdeclaredfields ();    //set permissions to access private fields    accessibleobject.setaccessible (fields,true);    Get an XML element list with a specific name    list lis = root.elements ("Arsubitem"),    //the child element under the "Arsubitem" element &Nbsp;  for (int i=0;i<lis.size (); i++) {    element element= (Element) lis.get (i);     iterator iter=element.elementiterator ();     //instantiating objects from constructors      serializable localobj= (Serializable) con.newinstance (null);     //cycle by number of fields     int J=0;    while (Iter.hasnext ()) {      Element e= (Element) Iter.next ();      fields[j].set (Localobj,e.gettext ());      j++;    }    list.add (LOCALOBJ);    }   }catch (Exception e) {   e.printstacktrace ();   }  return list; &NBSP}}


By modifying the prototype of the above code, the requirement of paging and result set nesting can be realized. In writing this article, I stumbled upon the Jakarta Commons Beanutils also provides similar functionality. So this article is just as an example of understanding reflection.


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.