[一]、简单bean的序列化和反序列化
1.java bean
Java代码
- package michael.serialization.simplexml;
- import java.util.Date;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- /**
- *
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class MyTestVo {
- @Element
- private String userName;
- @Attribute
- private String wife;
- @Attribute
- private String realName;
- @Element
- private Date bornDate;
- @Element
- private Double height;
- public String toString() {
- return "MyTestVo : [ userName = " + userName + " , wife = " + wife
- + " , realName = " + realName + " , height = " + height
- + " , bornDate = " + bornDate + " ]";
- }
- //省略set get等方法
- ......
- }
2.序列化
- public static void main(String[] args) throws Exception {
- String xmlpath = "d:/test/michael/simple_testvo.xml";
- MyTestVo vo = new MyTestVo();
- vo.setUserName("michael");
- vo.setRealName("大大");
- vo.setWife("小小");
- vo.setHeight(173.3d);
- vo.setBornDate(new Date());
- try {
- Serializer serializer = new Persister();
- File result = new File(xmlpath);
- serializer.write(vo, result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
序列化成功生成的simple_testvo.xml文件如下:
Xml代码
- <myTestVo wife="小小" realName="大大">
- <userName>michael</userName>
- <bornDate>2011-09-28 17:39:59.432 CST</bornDate>
- <height>173.3</height>
- </myTestVo>
ps: 注解可以把Java的属性序列化时指定为属性或者节点元素
3.反序列化
把上述生成的XML文件反序列化成Java bean测试代码:
- public static void main(String[] args) throws Exception {
- String xmlpath = "d:/test/michael/simple_testvo.xml";
- Serializer serializer = new Persister();
- File source = new File(xmlpath);
- try {
- MyTestVo vo = serializer.read(MyTestVo.class, source);
- System.out.println(vo);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
如果XML中包括中文字符有可能反序列化时会报错,以utf-8的编码读取XML文件即可,故修改代码如下:
Java代码
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- String xmlpath = "d:/test/michael/simple_testvo.xml";
- Serializer serializer = new Persister();
- try {
- InputStreamReader is = new InputStreamReader(new FileInputStream(
- xmlpath), "utf-8");
- PropertyList parseVo = serializer.read(PropertyList.class, is);
- System.out.println(parseVo);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行反序列化,打印Java bean信息如下:
MyTestVo : [ userName = michael , wife = 小小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 17:39:59 CST 2011 ]
[二]、自定义节点名称
1.java bean
Java代码
- package michael.serialization.simplexml;
- import java.util.Date;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root(name = "MyTest")
- public class MyTestVo {
- @Element
- private String userName;
- @Attribute(name = "MyWife")
- private String wife;
- @Attribute
- private String realName;
- @Element(name = "born")
- private Date bornDate;
- @Element
- private Double height;
- @Override
- public String toString() {
- return "MyTestVo : [ userName = " + userName + " , wife = " + wife
- + " , realName = " + realName + " , height = " + height
- + " , bornDate = " + bornDate + " ]";
- }
- //set get ......
- }
2.序列化
序列化后生成的simple_testvo.xml文件如下:
Xml代码
- <MyTest MyWife="小小" realName="大大">
- <userName>michael</userName>
- <born>2011-09-28 21:47:37.455 CST</born>
- <height>173.3</height>
- </MyTest>
可以和之前的序列化XML文件对比下,看看区别在哪里。
3.反序列化
运行反序列化程序后的打印结果如下:
MyTestVo : [ userName = michael , wife = 小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 21:47:37 CST 2011 ]
[三]、嵌套对象
1.java bean
Java代码
- package michael.serialization.simplexml;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class ConfigurationVo {
- @Element
- private ServerVo server;
- @Attribute
- private int id;
- public ServerVo getServer() {
- return server;
- }
- public int getId() {
- return id;
- }
- public void setServer(ServerVo pServer) {
- server = pServer;
- }
- public void setId(int pId) {
- id = pId;
- }
- }
Java代码
- package michael.serialization.simplexml;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class ServerVo {
- @Attribute
- private int port;
- @Element
- private String host;
- @Element
- private SecurityVo security;
- public int getPort() {
- return port;
- }
- public String getHost() {
- return host;
- }
- public SecurityVo getSecurity() {
- return security;
- }
- public void setPort(int pPort) {
- port = pPort;
- }
- public void setHost(String pHost) {
- host = pHost;
- }
- public void setSecurity(SecurityVo pSecurity) {
- security = pSecurity;
- }
- }
Java代码
- package michael.serialization.simplexml;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class SecurityVo {
- @Attribute
- private boolean ssl;
- @Element
- private String keyStore;
- public boolean isSsl() {
- return ssl;
- }
- public String getKeyStore() {
- return keyStore;
- }
- public void setSsl(boolean pSsl) {
- ssl = pSsl;
- }
- public void setKeyStore(String pKeyStore) {
- keyStore = pKeyStore;
- }
- }
2.序列化
Java代码
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- String xmlpath = "d:/test/michael/simple_testvo.xml";
- SecurityVo security = new SecurityVo();
- security.setSsl(true);
- security.setKeyStore("Michael");
- ServerVo server = new ServerVo();
- server.setHost("sjsky.iteye.com");
- server.setPort(8088);
- server.setSecurity(security);
- ConfigurationVo config = new ConfigurationVo();
- config.setId(10000);
- config.setServer(server);
- Serializer serializer = new Persister();
- try {
- File xmlFile = new File(xmlpath);
- serializer.write(config, xmlFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
运行上述方法,序列化生成的XML文件如下:
Xml代码
- <configurationVo id="10000">
- <server port="8088">
- <host>sjsky.iteye.com</host>
- <security ssl="true">
- <keyStore>Michael</keyStore>
- </security>
- </server>
- </configurationVo>
3.反序列化的方法和之前的一致,自己 可以 测试下结果是否正确。