posts - 37,comments - 7,trackbacks - 0

          import java.lang.reflect.Array;
          import java.lang.reflect.Field;
          import java.lang.reflect.Modifier;
          import java.util.IdentityHashMap;
          import java.util.Map;

          import org.jdom.Document;
          import org.jdom.Element;

          public class Driver {
              public static Document serializeObject(Object source) throws Exception {
                  return serializeHelper(source, new Document(new Element("serialized")),
                          new IdentityHashMap());
              }

              private static Document serializeHelper(Object source, Document target,
                      Map table) throws Exception {
                  String id = Integer.toString(table.size());
                  table.put(source, id);
                  Class sourceclass = source.getClass();
                  Element oElt = new Element("object");
                  oElt.setAttribute("class", sourceclass.getName());
                  oElt.setAttribute("id", id);
                  target.getRootElement().addContent(oElt);
                  if (!sourceclass.isArray()) {
                      Field[] fields = Mopex.getInstanceVariables(sourceclass);
                      for (int i = 0; i < fields.length; i++) {
                          if (!Modifier.isPublic(fields[i].getModifiers()))
                              fields[i].setAccessible(true);
                          Element fElt = new Element("field");
                          fElt.setAttribute("name", fields[i].getName());
                          Class declClass = fields[i].getDeclaringClass();
                          fElt.setAttribute("declaringclass", declClass.getName());
                          Class fieldtype = fields[i].getType();
                          Object child = fields[i].get(source);
                          if (Modifier.isTransient(fields[i].getModifiers())) {
                              child = null;
                          }
                          fElt.addContent(serializeVariable(fieldtype, child, target,
                                  table));
                          oElt.addContent(fElt);
                      }
                  } else {
                      Class componentType = sourceclass.getComponentType();
                      int length = Array.getLength(source);
                      oElt.setAttribute("length", Integer.toString(length));
                      for (int i = 0; i < length; i++) {
                          oElt.addContent(serializeVariable(componentType, Array.get(
                                  source, i), target, table));
                      }
                  }
                  return target;
              }

              private static Element serializeVariable(Class fieldtype, Object child,
                      Document target, Map table) throws Exception {
                  if (child == null) {
                      return new Element("null");
                  } else if (!fieldtype.isPrimitive()) {
                      Element reference = new Element("reference");
                      if (table.containsKey(child)) {
                          reference.setText(table.get(child).toString());
                      } else {
                          reference.setText(Integer.toString(table.size()));
                          serializeHelper(child, target, table);
                      }
                      return reference;
                  } else {
                      Element value = new Element("value");
                      value.setText(child.toString());
                      return value;
                  }
              }
          }

          ===============================
          import java.lang.reflect.Field;
          import java.lang.reflect.Method;
          import java.lang.reflect.Modifier;
          import java.util.LinkedList;
          import java.util.List;

          public class Mopex {
              public static Method getSupportedMethod(Class cls, String name,
                      Class[] paramTypes) throws NoSuchMethodException {
                  if (cls == null) {
                      throw new NoSuchMethodException();
                  }
                  try {
                      return cls.getDeclaredMethod(name, paramTypes);
                  } catch (NoSuchMethodException ex) {
                      return getSupportedMethod(cls.getSuperclass(), name, paramTypes);
                  }
              }

              public static Field[] getInstanceVariables(Class cls) {
                  List accum = new LinkedList();
                  while (cls != null) {
                      Field[] fields = cls.getDeclaredFields();
                      for (int i = 0; i < fields.length; i++) {
                          if (!Modifier.isStatic(fields[i].getModifiers())) {
                              if (!Modifier.isPublic(fields[i].getModifiers()))
                                  fields[i].setAccessible(true);
                              accum.add(fields[i]);
                          }
                      }
                      cls = cls.getSuperclass();
                  }
                  Field[] retvalue = new Field[accum.size()];
                  return (Field[]) accum.toArray(retvalue);
              }
          }

          ===============================

          public class Animal {
              private String name;
              private String gender;
              private String classification;
              private int weight;
              public Animal(String name,String gender, String classification,  int weight) {
                  super();
                  this.classification = classification;
                  this.gender = gender;
                  this.name = name;
                  this.weight = weight;
              }

          }

          ==============================
          import java.util.ArrayList;
          import java.util.List;

          public class Zoo {
              private String city;

              private String name;

              public Zoo(String name, String city) {

                  this.city = city;
                  this.name = name;
              }

              List animals = new ArrayList();

              public void add(Animal panda1) {
                  animals.add(animals);
              }

          }
          =========================
          import org.jdom.Document;
          import org.jdom.output.XMLOutputter;

          public class ZooTest {
              public static void main(String[] args) {
                  Animal panda1 = new Animal("Tian Tian", "male",
                          "Ailuropoda melanoleuca", 271);
                  Animal panda2 = new Animal("Mei Xiang", "female",
                          "Ailuropoda melanoleuca", 221);
                  Zoo national = new Zoo("National Zoological Park", "Washington, D.C.");
                  national.add(panda1);
                  national.add(panda2);
                  try {
                      XMLOutputter out = new XMLOutputter();
                      Document d = Driver.serializeObject(national);
                      out.output(d, System.out);
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }
              }
          }



          posted on 2005-08-17 09:35 Dave 閱讀(162) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 邹城市| 斗六市| 库伦旗| 平邑县| 长子县| 北流市| 界首市| 四会市| 马鞍山市| 泾源县| 凉山| 丰原市| 上犹县| 杭州市| 北宁市| 三亚市| 阆中市| 常州市| 鄂州市| 九江市| 娄底市| 会东县| 都江堰市| 邵东县| 罗江县| 白河县| 揭东县| 威海市| 哈巴河县| 从江县| 永平县| 池州市| 怀安县| 镇巴县| 安溪县| 洞口县| 嵩明县| 铁岭县| 云梦县| 中方县| 阿坝县|