1 56 57 package org.jdom.contrib.beans; 58 59 import org.jdom.*; 60 import org.jdom.output.*; 61 import org.jdom.input.*; 62 import java.util.*; 63 64 public class TestBean implements java.io.Serializable { 65 private String name; 66 private int age; 67 private Date birthdate; 68 private TestBean friend; 69 70 public String getName() { 71 return name; 72 } 73 public int getAge() { 74 return age; 75 } 76 public Date getBirthdate() { 77 return birthdate; 78 } 79 public TestBean getFriend() { 80 return friend; 81 } 82 83 public void setName(String name) { 84 this.name = name; 85 } 86 public void setAge(int age) { 87 this.age = age; 88 } 89 public void setBirthdate(Date birthdate) { 90 this.birthdate = birthdate; 91 } 92 public void setFriend(TestBean friend) { 93 this.friend = friend; 94 } 95 96 public String toString() { 97 return "TestBean[name='" + name + "', age=" + age + ", birthdate=" + birthdate + ", friend=" + friend + "]"; 98 } 99 100 101 103 public static void main(String [] args) throws java.beans.IntrospectionException , java.io.IOException { 104 105 try { 106 BeanMapper mapper = new BeanMapper(); 107 mapper.addMapping("birthdate", "dob"); mapper.addMapping("age", "dob", "age"); 110 mapper.setBeanPackage("org.jdom.contrib.beans"); 111 112 114 TestBean alex = new TestBean(); 115 alex.setName("Alex"); 116 alex.setAge(31); 117 alex.setBirthdate(new Date(69, 7, 8)); 118 119 TestBean amy = new TestBean(); 120 amy.setName("Amy"); 121 amy.setAge(25); 122 amy.setBirthdate(new Date(75, 4, 1)); 123 124 alex.setFriend(amy); 125 126 Document doc = mapper.toDocument(alex); 127 XMLOutputter o = new XMLOutputter(Format.getPrettyFormat()); 128 o.output(doc, System.out); 129 System.out.println(); 130 131 TestBean test2 = (TestBean)mapper.toBean(doc); 133 System.out.println(test2); 134 } 135 catch (BeanMapperException e) { 136 e.printStackTrace(); 137 } 138 } 139 140 } 141 | Popular Tags |