ip-1000

少说、多听、多做、多思考!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
/**
     * 将el下所有子节点值封装到实体类
     * 
@param el 根节点
     * 
@param obj 实体类
     
*/
    
private static void getXMLNodeToEntity(XmlElement el, Object obj) {
         List
<XmlElement> childList = new ArrayList<XmlElement>();
         childList 
= el.children();
         Class
<?> c = obj.getClass();
         
for(XmlElement xel : childList)
         {
             
try {
                    Method method;
                    
try {
                        Method[] m 
= c.getMethods();
                        
for (int i = 0; i < m.length; i++) {   
                            
if(m[i].getName().equals("set"+xel.tagName())){
                                method 
= m[i];
                                String type 
= c.getDeclaredField(xel.tagName()).getType().getName();
                                
if(type.contains(".")){
                                    String[] s
=type.split("\\.");
                                    
if("Date".equals(s[2])){
                                        
if(!xel.stringValue().isEmpty()){
                                            method.invoke(obj,  xel.dateValue());
                                        }
                                    }
else{
                                        
if(!xel.stringValue().isEmpty()){
                                            method.invoke(obj, xel.stringValue());
                                        }
                                    }
                                }
else{
                                    
if("double".equals(type)){
                                        
if(!xel.stringValue().isEmpty()){
                                            method.invoke(obj,  xel.doubleValue());
                                        }
                                    }
else if("short".equals(type)){
                                            
if(!xel.stringValue().isEmpty()){
                                                method.invoke(obj,  xel.shortValue());
                                            }
                                    }
else{
                                        
if(!xel.stringValue().isEmpty()){
                                            method.invoke(obj,  xel.intValue());
                                        }
                                    }
                                        
                                }
                            }
                        }
                    } 
catch (SecurityException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
catch (IllegalArgumentException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
catch (DOMException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
catch (IllegalAccessException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
catch (InvocationTargetException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            } 
catch (SecurityException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            } 
catch (NoSuchFieldException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
    }
posted on 2009-10-29 15:39  ip-10000  阅读(524)  评论(0编辑  收藏  举报