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:
/** * @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.
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.