[四]、可选的非强制性的元素或属性
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(required = false)
- private String wife;
- @Attribute
- private String realName;
- // 不想泄露年龄噢
- @Element(required = false)
- private Date bornDate;
- @Element
- private Double height;
- @Override
- public String toString() {
- return "MyTestVo : [ userName = " + userName + " , wife = " + wife
- + " , realName = " + realName + " , height = " + height
- + " , bornDate = " + bornDate + " ]";
- }
- //省略setter getter方法
- }
2.序列化
Java代码
- /**
- * @param args
- * @throws Exception
- */
- 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.setHeight(173.3d);
- Serializer serializer = new Persister();
- try {
- File xmlFile = new File(xmlpath);
- serializer.write(vo, xmlFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
运行序列化程序后生成的XML文件如下:
Xml代码
- <myTestVo realName="大大">
- <userName>michael</userName>
- <height>173.3</height>
- </myTestVo>
3.反序列化
运行反序列化程序后打印结果如下:
MyTestVo : [ userName = michael , wife = null , realName = 大大 , height = 173.3 , bornDate = null ]
[五]、List<Object>处理
1.java bean
Java代码
- package michael.serialization.simplexml;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.ElementList;
- import org.simpleframework.xml.Root;
- import org.simpleframework.xml.Serializer;
- import org.simpleframework.xml.core.Persister;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class PropertyList {
- @ElementList
- private List<EntryVo> list;
- @Attribute
- private String name;
- public List<EntryVo> getList() {
- return list;
- }
- public String getName() {
- return name;
- }
- public void setList(List<EntryVo> pList) {
- list = pList;
- }
- public void setName(String pName) {
- name = pName;
- }
- @Override
- public String toString() {
- return "PropertyList : [ name = " + name + " , EntryVo list size = "
- + list.size() + " ] .";
- }
- }
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 EntryVo {
- @Attribute
- private String name;
- @Element
- private String value;
- public String getName() {
- return name;
- }
- public String getValue() {
- return value;
- }
- public void setName(String pName) {
- name = pName;
- }
- public void setValue(String pValue) {
- value = pValue;
- }
- }
2.序列化
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 {
- PropertyList vo = initBean();
- serializer.write(vo, new File(xmlpath));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static PropertyList initBean() {
- PropertyList vo = new PropertyList();
- vo.setName("Wife List");
- List<EntryVo> subList = new ArrayList<EntryVo>();
- EntryVo subvo = new EntryVo();
- subvo.setName("A");
- subvo.setValue("福晋");
- subList.add(subvo);
- subvo = new EntryVo();
- subvo.setName("B");
- subvo.setValue("侧福晋");
- subList.add(subvo);
- subvo = new EntryVo();
- subvo.setName("C");
- subvo.setValue("小三");
- subList.add(subvo);
- subvo = new EntryVo();
- subvo.setName("D");
- subvo.setValue("二奶");
- subList.add(subvo);
- vo.setList(subList);
- return vo;
- }
运行序列化程序后生成的XML文件如下:
Xml代码
- <propertyList name="Wife List">
- <list class="java.util.ArrayList">
- <entryVo name="A">
- <value>福晋</value>
- </entryVo>
- <entryVo name="B">
- <value>侧福晋</value>
- </entryVo>
- <entryVo name="C">
- <value>小三</value>
- </entryVo>
- <entryVo name="D">
- <value>二奶</value>
- </entryVo>
- </list>
- </propertyList>
3.反序列化,运行结果打印对象信息如下:
PropertyList : [ name = Wife List , EntryVo list size = 4 ] .
4.修改注解@ElementList的参数
Java代码
- @ElementList(name = "WifeList", entry = "wife")
- private List<EntryVo> list;
序列化后生成的XML文件如下:
Xml代码
- <propertyList name="Wife List">
- <WifeList class="java.util.ArrayList">
- <wife name="A">
- <value>福晋</value>
- </wife>
- <wife name="B">
- <value>侧福晋</value>
- </wife>
- <wife name="C">
- <value>小三</value>
- </wife>
- <wife name="D">
- <value>二奶</value>
- </wife>
- </WifeList>
- </propertyList>
注意XML文件的变化。
[六]、 inline 参数用法
1.java bean
以上节中得bean为基础修改注解如下:
Java代码
- @Root
- public class PropertyList {
- @ElementList(name = "WifeList", entry = "wife", inline = true)
- private List<EntryVo> list;
- @Attribute
- private String name;
- public List<EntryVo> getList() {
- return list;
- }
- public String getName() {
- return name;
- }
- public void setList(List<EntryVo> pList) {
- list = pList;
- }
- public void setName(String pName) {
- name = pName;
- }
- @Override
- public String toString() {
- return "PropertyList : [ name = " + name + " , EntryVo list size = "
- + list.size() + " ] .";
- }
- }
2.序列化后生成的XML文件如下:
Java代码
- <propertyList name="Wife List">
- <wife name="A">
- <value>福晋</value>
- </wife>
- <wife name="B">
- <value>侧福晋</value>
- </wife>
- <wife name="C">
- <value>小三</value>
- </wife>
- <wife name="D">
- <value>二奶</value>
- </wife>
- </propertyList>
和上节生成的文件相比,XML结构少了一个层次。
[七]、构造函数的注解处理
1.java bean
Java代码
- package michael.serialization.simplexml;
- import org.simpleframework.xml.Attribute;
- import org.simpleframework.xml.Element;
- import org.simpleframework.xml.Root;
- import org.simpleframework.xml.Serializer;
- import org.simpleframework.xml.core.Persister;
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Root
- public class EntryVo {
- public EntryVo(@Attribute(name = "name")
- String name, @Element(name = "value")
- String value) {
- this.name = name;
- this.value = value;
- }
- @Attribute(name = "name")
- private String name;
- @Element(name = "value")
- private String value;
- public String getName() {
- return name;
- }
- public String getValue() {
- return value;
- }
- public void setName(String pName) {
- name = pName;
- }
- public void setValue(String pValue) {
- value = pValue;
- }
- @Override
- public String toString() {
- return "EntryVo : [ name = " + name + ", value = " + value + " ].";
- }
- }
2.序列化
生成的XML文件如下:
<entryVo name="blog">
<value>http://sjsky.iteye.com</value>
</entryVo>
3.反序列化
反序列化生成的bean的信息打印如下:
EntryVo : [ name = blog, value = http://sjsky.iteye.com ].
ps:如果java bean有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。