- 浏览: 149593 次
- 性别:
- 来自: 五指山
文章分类
最新评论
-
dengdonglin888:
qq_30221445 写道你好 请问这种数据能解吗 < ...
Simple XML -
qq_30221445:
你好 请问这种数据能解吗 <request> ...
Simple XML -
画个逗号给明天qu:
画个逗号给明天qu 写道
Android上传文件到服务器 -
画个逗号给明天qu:
...
Android上传文件到服务器 -
alranger:
我在jsp页面加上这一段代码后,问题还是存在。
解决Ext在ie9报错:不支持extjs对象的“createContextualFragment属性或方法”
之前曾写过一blog : XStream序列化JAVA对象为XML以及反序列化 (http://sjsky.iteye.com/blog/784434),今天介绍另一个Java Bean<->XML 之间序列化和反序列化的轻量级工具:Simple
官网:http://simple.sourceforge.net/home.php
截止目前最新版本(附近可下载):simple-xml-2.6.1.jar
特点:
- jar lib文件只有360K左右的大小
- 它的使用不需要依赖于其他 JAR 文件
- 通过注解的方式,灵活方便
下面将分节详细介绍Simple的特点和使用方法:
- [一]、简单bean的序列化和反序列化
- [二]、自定义节点名称
- [三]、嵌套对象
- [四]、可选的非强制性的元素或属性
- [五]、List<Object>处理
- [六]、inline 参数用法
- [七]、构造函数的注解处理
[一]、简单bean的序列化和反序列化
1.java bean
- 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文件如下:
- <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文件即可,故修改代码如下:
- /**
- * @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信息如下:
[二]、自定义节点名称
1.java bean
- 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文件如下:
- <MyTest MyWife="小小" realName="大大">
- <userName>michael</userName>
- <born>2011-09-28 21:47:37.455 CST</born>
- <height>173.3</height>
- </MyTest>
可以和之前的序列化XML文件对比下,看看区别在哪里。
3.反序列化
运行反序列化程序后的打印结果如下:
[三]、嵌套对象
1.java bean
- 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;
- }
- }
- 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;
- }
- }
- 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.序列化
- /**
- * @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文件如下:
- <configurationVo id="10000">
- <server port="8088">
- <host>sjsky.iteye.com</host>
- <security ssl="true">
- <keyStore>Michael</keyStore>
- </security>
- </server>
- </configurationVo>
3.反序列化的方法和之前的一致,自己 可以 测试下结果是否正确。
[四]、可选的非强制性的元素或属性
1.java bean
- 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.序列化
- /**
- * @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文件如下:
3.反序列化
运行反序列化程序后打印结果如下:
[五]、List<Object>处理
1.java bean
- 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() + " ] .";
- }
- }
- 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.序列化
- /**
- * @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文件如下:
- <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.反序列化,运行结果打印对象信息如下:
4.修改注解@ElementList的参数
序列化后生成的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为基础修改注解如下:
- @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文件如下:
- <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
- 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文件如下:
<value>http://sjsky.iteye.com</value>
</entryVo>
3.反序列化
反序列化生成的bean的信息打印如下:
ps:如果java bean有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。
本文就先介绍到这,下次再介绍其他运用事例。
本文连接:http://sjsky.iteye.com/blog/1182057
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------
评论
<request>
<param name="_input_charset">utf-8</param>
<param name="service">single_trade_query</param>
<param name="partner">20889010000000</param>
<param name="out_trade_no">7_20130223150319_26</param>
</request>
?
<param name="_input_charset">utf-8</param>就是一个字段有属性又有元素的情况
<request>
<param name="_input_charset">utf-8</param>
<param name="service">single_trade_query</param>
<param name="partner">20889010000000</param>
<param name="out_trade_no">7_20130223150319_26</param>
</request>
?
<param name="_input_charset">utf-8</param>就是一个字段有属性又有元素的情况
发表评论
-
tesseract-ocr识别中文扫描图片
2013-06-26 16:58 1557http://code.google.com/p/tesse ... -
java获取图片每个像素点的RGB
2013-06-26 16:46 2328http://tinyking.blog.51cto.com/ ... -
Java读取图片EXIF信息
2013-06-26 16:44 540http://hhhk.iteye.com/blog/109 ... -
Java之美[从菜鸟到高手演变]之设计模式
2012-12-10 10:12 732http://blog.csdn.net/zhangerqin ... -
GMT时间对应的解析式
2012-06-21 14:59 669ddd MMM dd HH:mm:ss K yyyy &qu ... -
简易excel读取开发包
2012-06-06 23:29 626http://fastexcel.sourceforge.ne ... -
关于java按字节截取带有汉字的字符串的解法
2012-05-09 23:43 813转自:http://lanxia39.iteye.com/bl ... -
Java线程之线程同步
2012-05-09 23:37 565转自:http://blog.csdn.net/mq612 ... -
JAVA修饰符类型(public,protected,private,friendly)的通易解释
2012-05-07 18:51 671JAVA修饰符类型(public,protected,pr ... -
POI
2011-11-30 17:15 627转自:http://zmx.iteye.com/blog/62 ... -
使用poi读写excel文件
2011-11-30 14:31 641http://cangzhitao.com/java/poi- ... -
JAVA集合类
2011-11-28 22:10 666list:arraylist,vector,linkedlis ... -
POI的使用实例说明
2011-11-28 16:56 978转自:http://zmx.iteye.com/blog/62 ... -
java集合类总结
2011-11-28 09:53 654对象的集合 如果程序的对象数量有限,且寿命可知,那么这个程序是 ... -
[JAVA]Excel 文档操作 .
2011-11-25 11:27 768转自:http://blog.csdn.net/s ...
相关推荐
SAX解析xml数据。比较麻烦的是,每份需要解析的xml数据对应的都需要创建相应的DefaultHandler2类,如果App后台接口较多的话,需要创建的解析后台数据的DefaultHandler2类也就...这种情况下建议还是使用simple-xml解析
java开源软件
赠送jar包:simple-xml-safe-2.7.1.jar; 赠送原API文档:simple-xml-safe-2.7.1-javadoc.jar; 赠送源代码:simple-xml-safe-2.7.1-sources.jar; 赠送Maven依赖信息文件:simple-xml-safe-2.7.1.pom; 包含翻译后...
simple-xml-2.6.2.jar主要用于模拟报文的类库,有需要的可以下载
a simple xml dom parser
simple-xml-safe-2.7.1.jar
dsx(Java的Damn Simple Xml) 该库利用方法链。 它与Java XML Standard API不兼容。 它包含与Microsoft XmlSerializer输入/输出XML格式兼容的序列化程序。 它包含另一个Microsoft兼容的序列化器,其行为类似于...
simple-xml解析xml jar包
Simple-XML-PrefixHack 将Hacking前缀为Simple XML Android Framework,现在该库接受带有名称间隔的重复元素。
最高效的XML解析方式-----Simple 简化 XML 解析jar包simple-xml-2.7.1.rar
simple-xml是用于解析xml的开源项目,是目前使用较广的,他的执行效率较高,下载包含simple-xml-2.7.1所需要的jar包,doc以及demo.免费。
因为是一个简单的XML留言簿,所以取名SXGB(Simple XML GuestBook)。留言本演示,管理密码为test:http://home.goofar.com/hotheart/gbook/gbook.asp首先定义留言簿的XML文档的格式。作为一个留言簿,不需要太复杂的...
一个简单的xml文件分析控件,很适用。 全部源代码。
simple-xml-2.3.4.jar非常好用的序列化java类的工具类包
该死的简单XML Dams Simple XML DSX经过优化,可以序列化JavaScript对象并反序列化以前从对象以其他编程语言(或通过DSX)进行序列化的XML元素。 DSX不能处理任何形式的自由格式XML文档。 DSX的内存占用量很小。 它...
包含simple和parser 很好用的xml解析文件
IBM在线教程,教程探讨 Simple API for XML 2.0.x 版(或 SAX 2.0.x)的使用。它针对的是如下开发人员,他们理解 XML,并且希望学习这个用于操作 XML 数据的轻量级的、基于事件的 API。 这里假设读者熟悉诸如格式...
Simple 是一个 Java 框架,用于简化序列化和反序列化 XML 的过程。使用 Simple,开发人员可以简化(名字由此而来)将简单老 Java 对象(POJO)转换成 XML 文档的过程 — 即所谓的序列化(serialization)过程。...
side your compiled code, I have always preferred simple abstraction either using roles and their corresponding mappings in the database or using simple xml file to store action to role mappings. ...
XML-Simple-2.20 perl开发使用