java 反射的应用(将一个java对象转换成一个xml文件)

将一个java对象转换成xml文件,这个应用是在做一些异步交互中所需要的应用(例如:flex,extjs,ajax),当然现在已有一些架包可以将一个java对象转换成xml文件了,对于一些想知道java反射有什么应用让人可以看看下面的例子。

 

package com.test.flex.servlet;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ObjectToxml {
 
 
 public String change(Object obj, int count){
  StringBuffer sb =new StringBuffer();
  
  try {
   
   String objname = obj.getClass().getName();
   objname=objname.substring(objname.lastIndexOf(".")+1);
   if(count==1){ 
    sb.append("/n");
    sb.append("<"+objname+">/n");
    
   }else{
    sb.append("/t<"+objname+">/n");
   }
   System.out.println(obj.getClass().getName());
   Class objclass = Class.forName(obj.getClass().getName());
   
   Field [] objfileds =objclass.getDeclaredFields();
   
   
   for (Field field : objfileds) {
    System.out.println(field.getType().getName()+""+field.getName());
    String fieldname=field.getName();
    String name="get"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1);
    
    Method getobj = objclass.getMethod(name);
   
    
    if("java.util.List".equals(field.getType().getName())){
     Object o =getobj.invoke(obj);
     
     List list = (List) o;
     count++;
     for (Object object : list) {
      sb.append(change(object,count));
     }
     
     System.out.println(list.size());
    }
    else{
     Object a = getobj.invoke(obj);
     if(count==2){
      sb.append("/t/t<"+fieldname+">"+a.toString()+"/n");
      System.out.println(a);
     }else{
      sb.append("/t<"+fieldname+">"+a.toString()+"/n");
      System.out.println(a);
     }       
    }  
   }
   if(count==2){
    sb.append("/t/n");
    count--;
    System.out.println("jjjjjjjjjjjjjjjjjjjjjj"+count);
   }else{
    
    sb.append("/n");
   }
   
   
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalArgumentException 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();
  }
  return sb.toString();
  
 }
 
 public static void main(String[] args) {
  PageModel pm = new PageModel();
  Person p = new Person();
  p.setAge(11);
  p.setName("zhangsan");
  p.setSex("男");
  
  Person p1 = new Person();
  p1.setAge(21);
  p1.setName("lisi");
  p1.setSex("男");
  Person p2 = new Person();
  p2.setAge(51);
  p2.setName("wangwu");
  p2.setSex("男");
  
  List list = new ArrayList();
  list.add(p);
  list.add(p1);
  list.add(p2);
  
  pm.setPage(1);
  pm.setPagesize(2);
  pm.setResult(list);
  System.out.println(new ObjectToxml().change(pm,1));
  
 }

}

 

你可能感兴趣的:(java基础)