Convert the data in the request to a formbean.
Final roamopenaf AF = (roamopenaf) xmlutil. xml2form (request,
Roamopenaf. Class );
Public class roamopenaf extends baseaf
Public class baseaf extends validatorform
Package com. maywide. oss. commons. utils;
Import java. Lang. Reflect. method;
Import java. Text. simpledateformat;
Import java. util. arraylist;
Import java. util. date;
Import java. util. iterator;
Import java. util. List;
Import java. util. locale;
Import javax. servlet. http. httpservletrequest;
Import org. Apache. commons. Lang. stringutils;
Import org. Apache. commons. Logging. log;
Import org. Apache. commons. Logging. logfactory;
Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. Input. saxbuilder;
/**
* Contains the method for converting XML format data
* @ Author user
*
*/
Public class xmlutil {
Private Static log = logfactory. getlog (xmlutil. Class );
/**
* Convert the XML data contained in the request to a list.
*
* @ Param request
* @ Param clazz
* @ Return
* @ Throws exception
*/
Public static list xml2list (httpservletrequest request, class clazz)
Throws exception {
List <Object> List = new arraylist <Object> ();
Try {
Saxbuilder sb = new saxbuilder ();
Document reqdoc = sb. Build (request. getinputstream ());
Element rootelement = reqdoc. getrootelement (); // obtain the root element.
Object object = NULL;
List datalist = rootelement. getchildren ("data ");
If (datalist = NULL | datalist. isempty ())
Return list;
Element example = (element) datalist. Get (0 );
List childexamplelist = example. getchildren ();
If (childexamplelist = NULL | childexamplelist. isempty ())
Return list;
String [] names = new string [childexamplelist. Size ()];
For (INT I = 0; I <childexamplelist. Size (); I ++ ){
Element element = (element) childexamplelist. Get (I );
If (element. getname () = NULL
| Element. getname (). Trim (). Equals (""))
Continue;
Names [I] = element. getname ();
}
For (iterator it = datalist. iterator (); it. hasnext ();){
Element childelement = (element) it. Next ();
Object = clazz. newinstance ();
For (INT I = 0; I <names. length; I ++ ){
Element E = childelement. getchild (Names [I]);
Xmlutil. setvalue (Names [I], E. gettext (), object );
}
List. Add (object );
}
} Catch (exception e ){
Throw E;
}
Return list;
}
/**
* Convert the data in the request to a formbean.
* @ Param request
* @ Param clazz
* @ Return
* @ Throws exception
*/
Public static object xml2form (httpservletrequest request, class clazz)
Throws exception {
Object object = NULL;
Try {
Saxbuilder sb = new saxbuilder ();
Document reqdoc = sb. Build (request. getinputstream ());
Element root = reqdoc. getrootelement ();
List list = root. getchildren ();
Object = clazz. newinstance ();
For (INT I = 0; I <list. Size (); I ++ ){
Element E = (element) list. Get (I );
String name = E. getname ();
String value = E. gettext ();
If (name = NULL | Name. Trim (). Equals (""))
Continue;
If (name. indexof (".")> = 0 ){
String [] subnames = pubutil. Split (name ,".");
If (subnames = NULL)
Continue;
Object subobject = object;
For (Int J = 0; j <subnames. length; j ++ ){
If (subnames [J] = NULL
| Subnames [J]. Trim (). Equals (""))
Continue;
Try {
If (j <subnames. Length-1 ){
String methodname = "get"
+ Subnames [J]. substring (0, 1)
. Touppercase ()
+ Subnames [J]. substring (1 );
Method getmethod = subobject. getclass ()
. Getdeclaredmethod (methodname,
New Class [] {});
Object TMP = subobject;
Subobject = getmethod. Invoke (subobject,
New object [] {});
Class type = getmethod. getreturntype ();
If (subobject = NULL ){
Subobject = type. newinstance ();
String setmethodname = "set"
+ Subnames [J]. substring (0, 1)
. Touppercase ()
+ Subnames [J]. substring (1 );
Method setmethod = TMP. getclass ()
. Getdeclaredmethod (
Setmethodname,
New Class [] {type
. Getclass ()});
Setmethod. Invoke (TMP, subobject );
}
} Else {
Xmlutil. setvalue (subnames [J], value, subobject );
}
} Catch (nosuchmethodexception nsme ){
Continue;
}
}
} Else {
Xmlutil. setvalue (name, value, object );
}
}
} Catch (exception e ){
Throw E;
}
Return object;
}
/**
* Set the attribute value named field in the object to value.
* @ Param Field
* Attributes to be set
* @ Param Value
* Value to be set
* @ Param object
* Set object
* @ Throws exception
*/
Public static void setvalue (string field, string value, object)
Throws exception {
If (field = NULL | field. Trim (). Equals (""))
Return;
String methodname = "set" + field. substring (0, 1). touppercase ()
+ Field. substring (1 );
Method [] Methods = object. getclass (). getmethods ();
For (INT I = 0; I <methods. length; I ++ ){
If (methodname. Equals (methods [I]. getname ())){
Class [] paramclass = methods [I]. getparametertypes ();
If (paramclass = NULL | paramclass. length! = 1)
Continue;
If (paramclass [0]. Equals (string. Class )){
Methods [I]. Invoke (object, new object [] {value });
} Else if (paramclass [0]. Equals (long. Class )){
If (value = NULL | value. Equals ("")){
Long templong = NULL;
Methods [I]. Invoke (object, new object [] {templong });
} Else {
Methods [I]. Invoke (object, long. valueof (value ));
}
} Else if (paramclass [0]. Equals (double. Class )){
If (value = NULL | value. Equals ("")){
Double tempdouble = NULL;
Methods [I]. Invoke (object, new object [] {tempdouble });
} Else {
Methods [I]. Invoke (object, double. valueof (value ));
}
} Else if (paramclass [0]. Equals (Java. util. Date. Class )){
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS", locale. US );
Date = NULL;
Try {
If (! Stringutils. isempty (value )){
If (! Value. Contains (":")){
// Because the formatter format is yyyy-mm-dd hh: mm: SS, the value must include "hour, minute, and second ".
// Otherwise, unknown source will appear and throw parseexception will occur.
// Here, we only make simple judgments.
Value = value + "00:00:00 ";
}
Date = formatter. parse (value );
}
}
Catch (exception ex ){
Log. Error ("parse date exception, Case:", ex );
}
Methods [I]. Invoke (object, new object [] {date });
}
Break;
}
}
}
}