Simple XML

官网:http://simple.sourceforge.net/home.php

截止目前最新版本:simple-xml-2.7.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信息如下:


MyTestVo : [ userName = michael , wife = 小小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 17:39:59 CST 2011 ]

[二]、自定义节点名称

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.反序列化

运行反序列化程序后的打印结果如下:


MyTestVo : [ userName = michael , wife = 小小 , realName = 大大 , height = 173.3 , bornDate = Wed Sep 28 21:47:37 CST 2011 ]

[三]、嵌套对象

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文件如下:


<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


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.反序列化,运行结果打印对象信息如下:


PropertyList : [ name = Wife List , EntryVo list size = 4 ] .

4.修改注解@ElementList的参数


    @ElementList(name = "WifeList", entry = "wife")
private List<EntryVo> list;

序列化后生成的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文件如下:


<entryVo name="blog">
<value>http://sjsky.iteye.com</value>
</entryVo>

3.反序列化

反序列化生成的bean的信息打印如下:


EntryVo : [ name = blog, value = http://sjsky.iteye.com ].

ps:如果java
bean有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。

本文转自:http://sjsky.iteye.com/blog/1182057

时间: 2024-08-26 04:30:56

Simple XML的相关文章

simple XML解析XML

写前端不会直接用js吧?而是用jquery,用php解析XML有点类似,有没有简单一些方法进行快速解析XML.有的,就是simpleXMLS.学完js有再学jQuery是不是很随意,这个一样. 模板XML: <?xml version="1.0" encoding="utf-8"?> <bookstore><book category="COOKING" id="id2"><title

xml文件解析基础和签名

0. 前言 参考文档1:使用dom4j读取xml文件的四种方式 参考文档2:dom4j解析xml文件 参考文档3:String 和 document 的相互转换总结 参考文档4: Java SE 6中 XML 数字签名的标准 Java 接口 参考文档5:XML Security with Digital Signature in JAVA 1.xml文件的解析 1.1 基本的jdk解析xml的原始api解析 声明 1.以下代码块要放到一个类中才能运行 dom类型为:org.w3c.dom.* 解

php simpleXML操作xml的用法

XML简介 XML是一种流行的半结构化文件格式,以一种类似数据库的格式存储数据.在实际应用中,一些简单的.安全性较低的数据往往使用 XML文件的格式进行存储.这样做的好处一方面可以通过减少与数据库的交互性操作提高读取效率,另一方面可以有效利用 XML的优越性降低程序的编写难度. PHP提供了一整套的读取 XML文件的方法,很容易的就可以编写基于 XML的脚本程序.本章将要介绍 PHP与 XML的操作方法,并对几个常用的 XML类库做一些简要介绍. 1 XML简介 XML是“可扩展性标识语言(eX

Python xml

第一部分:读 ######## ## # -*- coding:utf-8 -*- """ * User: not me * Date: 11-11-9 * Time: 13:20 * Desc: not easy for newer like me """ from  xml.dom import  minidom def get_attrvalue(node, attrname): return node.getAttribute(attrn

python解析xml文件操作的例子

python解析xml文件操作实例,操作XML文件的常见技巧. xml文件内容: <?xml version="1.0" ?> <!--Simple xml document__chapter 8--> <book> <title> sample xml thing </title> <author> <name> <first> ma </first> <last>

jquery 读取xml

1 <script type="text/javascript" src="jquery/jquery-1.11.3.min.js"></script> 2 <script type="text/javascript"> 3 //var url = "http://www.w3school.com.cn/example/xmle/simple.xml"; 4 var url = "

浏览器中的XML与JavaScript

浏览器中的XML与JavaScript 在处理XML前,你需要在JavaScript中获取它.这一部分展示了一些不同的方法用来在JavaScript中获取XML并且对它进行处理. XML的节点类型 在我们研究如何处理XML前,先来了解下XML中不同的节点及类型.如果是HTML,了解这些节点就没必要了,但由于XML的可扩展性和结构的不确定性,了解这些固有节点类型就显得犹为重要了. 下面是XML 中12种不同的节点(表格取自W3School中文站点): 节点类型 描述 Document 表示整个文档

php xml转为xml或者json

<?php class XmlToArray { private $xml; private $contentAsName="content" ; private $attributesAsName="attributes"; private $xml_array = array(); public function setXml( $xmlstr ) { $this->xml = $xmlstr ; return $this ; } public fu

【Java123】XML与JSON互相转化

http://www.bejson.com/json2javapojo/new/ https://www.cnblogs.com/111testing/p/9162229.html https://wenku.baidu.com/view/8364d04b86c24028915f804d2b160b4e777f8102.html https://www.jianshu.com/p/8dadd9a232b1 一个使用org.json的Demo: 1 import org.apache.common