- 浏览: 833353 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (363)
- 2010年3月 (3)
- 2010年4月 (3)
- Java (116)
- ExtJs (22)
- EJB3.0 (11)
- JQuery (28)
- SqlServer (5)
- Oracle (17)
- hibernate (21)
- struts2 (14)
- php (10)
- JavaScript (11)
- jbpm (6)
- spring (24)
- lucene (2)
- ibatis (7)
- C# (8)
- mysql (11)
- json (3)
- webservice (3)
- 设计模式 (1)
- jdbc (1)
- servlet (2)
- ice (6)
- 日常软件问题 (2)
- 生活 (2)
- iphone (1)
- rest (3)
- ruby (2)
- linux (13)
- quartz (1)
- poi (1)
- redis (13)
- memcached (4)
- nosql (2)
- tomcat调优 (1)
- 项目管理 (0)
最新评论
-
天使建站:
jquery里和数组相关的操作 附带具体的实例 ...
jquery对象数组 -
Cy0941:
$('#formId').form('submit',...) ...
easyui的form表单提交处理 -
shmily2038:
swifth 写道楼主,,你的命令写错啦,,[root@ser ...
centos直接yum安装nginx -
swifth:
楼主,,你的命令写错啦,,[root@server ~]# y ...
centos直接yum安装nginx -
随遇而安DXX:
...
REST
试了一下Thoughtworks的XStream,以测试驱动和敏捷开发著称的Thoughtworks,
写出的XStream果然很容易使用。
下面举一个用一个小例子说明如何使用:
实体Person,我们希望Person的对象能够和XML/JSON相互转化
Java代码
package edu.jlu.fuliang;
public class Person
{
private String name;
private int age;
private Address address;
private String email;
public Person()
{
}
public Person( String name, int age, Address address, String email )
{
this.name = name;
this.age = age;
this.address = address;
this.email = email;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age )
{
this.age = age;
}
public Address getAddress()
{
return address;
}
public void setAddress( Address address )
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail( String email )
{
this.email = email;
}
@Override
public String toString()
{
return "name:" + name + ",age:" + age + ",address:" + address + ",email:" + email;
}
}
package edu.jlu.fuliang;
public class Person
{
private String name;
private int age;
private Address address;
private String email;
public Person()
{
}
public Person( String name, int age, Address address, String email )
{
this.name = name;
this.age = age;
this.address = address;
this.email = email;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age )
{
this.age = age;
}
public Address getAddress()
{
return address;
}
public void setAddress( Address address )
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail( String email )
{
this.email = email;
}
@Override
public String toString()
{
return "name:" + name + ",age:" + age + ",address:" + address + ",email:" + email;
}
}
Java代码
package edu.jlu.fuliang;
public class Address
{
private String street;
private String city;
public Address()
{
}
public Address( String street, String city )
{
this.street = street;
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet( String street )
{
this.street = street;
}
public String getCity()
{
return city;
}
public void setCity( String city )
{
this.city = city;
}
@Override
public String toString()
{
return "street:" + street + ",city:" + city;
}
}
package edu.jlu.fuliang;
public class Address
{
private String street;
private String city;
public Address()
{
}
public Address( String street, String city )
{
this.street = street;
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet( String street )
{
this.street = street;
}
public String getCity()
{
return city;
}
public void setCity( String city )
{
this.city = city;
}
@Override
public String toString()
{
return "street:" + street + ",city:" + city;
}
}
写一个测试来看看如何使用XStream:
Java代码
package edu.jlu.fuliang;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamTest extends TestCase
{
private Person person;
@Override
protected void setUp() throws Exception
{
Address address = new Address("中关村","北京");
person = new Person("张三",12,address,"zhangsan@gmail.com");
}
public void testObjectXML()
{
XStream xStream = new XStream();
xStream.alias( "Person", Person.class);
xStream.alias( "Address", Address.class );
String xml = xStream.toXML( person );
System.out.println("Serialize person to XML:\n" + xml);
Person p = (Person) xStream.fromXML( xml );
System.out.println("Deserialize XML to person:\n" + p);
}
public void testObjectJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.setMode( XStream.NO_REFERENCES );
xStream.alias( "Person", Person.class );
xStream.alias("Address", Address.class);
String json = xStream.toXML( person );
System.out.println("Serialize person to JSON:\n" + json);
Person p = (Person) xStream.fromXML( json );
System.out.println("Deserialize XML to person:\n" + p);
}
}
package edu.jlu.fuliang;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamTest extends TestCase
{
private Person person;
@Override
protected void setUp() throws Exception
{
Address address = new Address("中关村","北京");
person = new Person("张三",12,address,"zhangsan@gmail.com");
}
public void testObjectXML()
{
XStream xStream = new XStream();
xStream.alias( "Person", Person.class);
xStream.alias( "Address", Address.class );
String xml = xStream.toXML( person );
System.out.println("Serialize person to XML:\n" + xml);
Person p = (Person) xStream.fromXML( xml );
System.out.println("Deserialize XML to person:\n" + p);
}
public void testObjectJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.setMode( XStream.NO_REFERENCES );
xStream.alias( "Person", Person.class );
xStream.alias("Address", Address.class);
String json = xStream.toXML( person );
System.out.println("Serialize person to JSON:\n" + json);
Person p = (Person) xStream.fromXML( json );
System.out.println("Deserialize XML to person:\n" + p);
}
}
结果:
引用
<Person>
<name>张三</name>
<age>12</age>
<address>
<street>中关村</street>
<city>北京</city>
</address>
<email>zhangsan@gmail.com</email>
</Person>
Serialize person to XML:
<Person>
<name>张三</name>
<age>12</age>
<address>
<street>中关村</street>
<city>北京</city>
</address>
<email>zhangsan@gmail.com</email>
</Person>
Deserialize XML to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com
Serialize person to JSON:
{"Person":{"name":"张三","age":12,"address":{"street":"中关村","city":"北京"},"email":"zhangsan@gmail.com"}}
Deserialize XML to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com
写出的XStream果然很容易使用。
下面举一个用一个小例子说明如何使用:
实体Person,我们希望Person的对象能够和XML/JSON相互转化
Java代码
package edu.jlu.fuliang;
public class Person
{
private String name;
private int age;
private Address address;
private String email;
public Person()
{
}
public Person( String name, int age, Address address, String email )
{
this.name = name;
this.age = age;
this.address = address;
this.email = email;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age )
{
this.age = age;
}
public Address getAddress()
{
return address;
}
public void setAddress( Address address )
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail( String email )
{
this.email = email;
}
@Override
public String toString()
{
return "name:" + name + ",age:" + age + ",address:" + address + ",email:" + email;
}
}
package edu.jlu.fuliang;
public class Person
{
private String name;
private int age;
private Address address;
private String email;
public Person()
{
}
public Person( String name, int age, Address address, String email )
{
this.name = name;
this.age = age;
this.address = address;
this.email = email;
}
public String getName()
{
return name;
}
public void setName( String name )
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge( int age )
{
this.age = age;
}
public Address getAddress()
{
return address;
}
public void setAddress( Address address )
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail( String email )
{
this.email = email;
}
@Override
public String toString()
{
return "name:" + name + ",age:" + age + ",address:" + address + ",email:" + email;
}
}
Java代码
package edu.jlu.fuliang;
public class Address
{
private String street;
private String city;
public Address()
{
}
public Address( String street, String city )
{
this.street = street;
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet( String street )
{
this.street = street;
}
public String getCity()
{
return city;
}
public void setCity( String city )
{
this.city = city;
}
@Override
public String toString()
{
return "street:" + street + ",city:" + city;
}
}
package edu.jlu.fuliang;
public class Address
{
private String street;
private String city;
public Address()
{
}
public Address( String street, String city )
{
this.street = street;
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet( String street )
{
this.street = street;
}
public String getCity()
{
return city;
}
public void setCity( String city )
{
this.city = city;
}
@Override
public String toString()
{
return "street:" + street + ",city:" + city;
}
}
写一个测试来看看如何使用XStream:
Java代码
package edu.jlu.fuliang;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamTest extends TestCase
{
private Person person;
@Override
protected void setUp() throws Exception
{
Address address = new Address("中关村","北京");
person = new Person("张三",12,address,"zhangsan@gmail.com");
}
public void testObjectXML()
{
XStream xStream = new XStream();
xStream.alias( "Person", Person.class);
xStream.alias( "Address", Address.class );
String xml = xStream.toXML( person );
System.out.println("Serialize person to XML:\n" + xml);
Person p = (Person) xStream.fromXML( xml );
System.out.println("Deserialize XML to person:\n" + p);
}
public void testObjectJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.setMode( XStream.NO_REFERENCES );
xStream.alias( "Person", Person.class );
xStream.alias("Address", Address.class);
String json = xStream.toXML( person );
System.out.println("Serialize person to JSON:\n" + json);
Person p = (Person) xStream.fromXML( json );
System.out.println("Deserialize XML to person:\n" + p);
}
}
package edu.jlu.fuliang;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class XStreamTest extends TestCase
{
private Person person;
@Override
protected void setUp() throws Exception
{
Address address = new Address("中关村","北京");
person = new Person("张三",12,address,"zhangsan@gmail.com");
}
public void testObjectXML()
{
XStream xStream = new XStream();
xStream.alias( "Person", Person.class);
xStream.alias( "Address", Address.class );
String xml = xStream.toXML( person );
System.out.println("Serialize person to XML:\n" + xml);
Person p = (Person) xStream.fromXML( xml );
System.out.println("Deserialize XML to person:\n" + p);
}
public void testObjectJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.setMode( XStream.NO_REFERENCES );
xStream.alias( "Person", Person.class );
xStream.alias("Address", Address.class);
String json = xStream.toXML( person );
System.out.println("Serialize person to JSON:\n" + json);
Person p = (Person) xStream.fromXML( json );
System.out.println("Deserialize XML to person:\n" + p);
}
}
结果:
引用
<Person>
<name>张三</name>
<age>12</age>
<address>
<street>中关村</street>
<city>北京</city>
</address>
<email>zhangsan@gmail.com</email>
</Person>
Serialize person to XML:
<Person>
<name>张三</name>
<age>12</age>
<address>
<street>中关村</street>
<city>北京</city>
</address>
<email>zhangsan@gmail.com</email>
</Person>
Deserialize XML to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com
Serialize person to JSON:
{"Person":{"name":"张三","age":12,"address":{"street":"中关村","city":"北京"},"email":"zhangsan@gmail.com"}}
Deserialize XML to person:
name:张三,age:12,address:street:中关村,city:北京,email:zhangsan@gmail.com
发表评论
-
extjs和jquery各适用于什么场景
2013-01-23 09:49 2075比较点 extjs jquery 是 ... -
extjs和jquery各适用于什么场景
2013-01-21 11:14 14151.应该说粒度不同,extjs ... -
sqlite 数据类型 全面
2011-06-14 13:49 1293一般数据采用的固定的 ... -
extjs direct下载地址
2010-09-11 18:23 1180http://code.google.com/p/direct ... -
json返回数据,首先要做的事
2010-09-08 12:45 1165var json=eval("("+jso ... -
ExtJS 面向对象
2010-08-25 15:15 10051:支持命名空间 <script type=&qu ... -
ExtJS border 布局
2010-08-25 14:42 1992border(边界) 布局是指,把面板组件显示在 北 东 南 ... -
Ext.data-DataProxy/HttpProxy/MemoryProxy/ScriptTagProxy
2010-08-03 09:15 1179Ext.data-DataProxy/HttpProxy/Me ... -
extjs表单的提交
2010-06-24 17:01 12764//ExtJs 表单提交 form.getForm(). ... -
JSONLib解析json数组
2010-06-24 15:33 1736public class JsonHelper { /** ... -
Jsonlib
2010-06-24 13:13 1361使用JSON-LIB可以极大的简化JAVA对象转换成JSON对 ... -
以"对象"方式接收json数据.(解决乱码问题)
2010-06-24 10:36 5427客户端(传输数据无中文): Java代码 //序号控件失去 ... -
extjs的xtype
2010-06-24 09:06 1321基本组件: xtype Class 描述 button E ... -
EXTJS 查询数据,分页显示时传送自定义参数,添加控件的任何事件模板
2010-06-21 13:25 2213EXTJS 查询数据,分页显示时传送自定义参数,添加控件的任何 ... -
Extjs 分页查询的参数问题
2010-06-21 12:57 2415先按条件查询数据,再点下一页保留住查询条件,解决方案是将查询参 ... -
EXT中grid分页实现的完整例子---从前台到后台
2010-05-25 00:14 13719// -------------------- 定义grid- ... -
ExtJs的el和contentEl的区别
2010-05-19 16:46 34351、把<div id="div_exam&qu ... -
Ext gird分页学习
2010-04-23 13:33 1062var store = new Ext.data.Store( ... -
SModel了解
2010-04-21 09:35 15211、比较 jsonlib,Xstream,gson,smode ... -
xstream实现JAVA对象和XML的相互转化
2010-04-20 21:56 2516首页 http://xstream.codehaus.org ...
相关推荐
XStream是一个开源库,它为Java对象提供了简单且直观的XML序列化和反序列化的解决方案。它不仅能够将Java对象转换成XML,反之亦然,还能支持JSON格式的转换。这个强大的工具在处理数据交换、持久化或配置文件时非常...
xStream是一个轻量级的Java库,用于将Java对象序列化为XML,同时也能将XML反序列化回Java对象。这个强大的工具同样支持JSON格式,使得在Java应用程序中处理XML和JSON数据变得非常便捷。本篇文章将深入探讨xStream...
XStream的核心功能是能够将Java对象和XML之间的映射自动化,极大地简化了序列化和反序列化的过程。你可以通过Maven或Gradle在项目中添加XStream依赖。例如,在Maven的pom.xml文件中,你可以添加如下依赖: ```xml ...
xStream是一个强大的库,它提供了一种简单的方式来序列化和反序列化Java对象到XML,反之亦然。在这个场景中,它同样能够处理JSON转换,虽然JSON不是其原生支持的格式,但通过一些额外的配置和库,如json-simple,...
XStream在运行时使用Java反射机制对要进行序列化的对象树的结构进行探索,并不需要对对象作出修改。XStream可以序列化内部字段,包括私private和final字段,并且支持非公开类以及内部类。 在缺省情况下,XStream不...
Xstream是一个Java库,它提供了简单、高效的方式来序列化和反序列化Java对象为XML,反之亦然。它的主要优点在于其简洁的API和可读性强的XML输出。在我们的示例"Xstream 类与Json之间转换"中,我们可能会看到如何利用...
此外,由于xStream默认的安全设置,直接反序列化不受信任的XML或JSON可能存在安全风险,因此在生产环境中,务必启用安全模式并使用适当的过滤器来保护应用程序。 总结来说,xStream是一个强大且灵活的工具,能够...
3. **xstream-1.3.1.jar**:XStream是另一个Java库,它提供了一种简单的方式来序列化和反序列化Java对象为XML。在XML到JSON转换过程中,XStream可能首先用于将XML数据转换为Java对象,然后这些对象再由Json-lib处理...
接下来,使用xStream进行XML序列化和反序列化非常简单。初始化xStream实例后,通过`toXML()`和`fromXML()`方法即可完成转换。 ```java XStream xstream = new XStream(); String xml = xStream.toXML(employee); ...
Xstream 是一个 Java 库,用于处理 XML 文件的序列化和反序列化,同时也支持将 JavaBean 转换为 JSON 格式。它的核心理念是通过简单的 API 设计,使得 XML 序列化过程变得简洁,无需额外的映射文件或辅助类。 ### 1...
同样,我们也可以使用XStream进行反序列化,将XML字符串恢复为Java对象: ```java String xmlInput = "<User><name>Forest</name><age>30</age></User>"; User deserializedUser = (User) xstream.fromXML(xmlInput)...
总之,"json XStream"涉及的主题是使用XStream库配合Jettison来实现Java对象与JSON格式之间的序列化和反序列化。通过理解这两个库的功能和如何整合它们,开发者可以有效地在XML和JSON之间进行数据交换,满足跨平台和...
同样,XStream也支持JSON序列化,只需要添加一个转换器: ```java xstream.useAttributeFor(Person.class, "age"); xstream.alias("person", Person.class); String json = xstream.toJSON(xml); ``` 反序列化过程也...
总之,Java对象的序列化和反序列化是开发中常见的需求,而Fastjson和XStream都是强大且高效的工具,可以方便地处理JSON和XML格式的数据。通过这两个库,开发者可以轻松地在Java对象和这些数据格式之间进行转换,满足...
2. **Gson**: 虽然Gson主要用于JSON序列化和反序列化,但通过扩展,也可以处理XML。首先将对象转换为JSON,然后使用像`javax.xml.transform.Transformer`这样的工具将JSON转换为XML。 3. **JAXB (Java Architecture...
总之,xStream是一个功能强大且灵活的工具,能够方便地处理Java对象与XML和JSON之间的转换,极大地简化了数据序列化和反序列化的任务,尤其是在互联网应用中,对于数据交换和存储具有很高的实用价值。
// JSON序列化 String json = xstream.toXML(bean); System.out.println(json); // JSON反序列化 bean = (Person) xstream.fromXML(json); System.out.println(bean); } } ``` 程序运行后,输出如下结果: ...
4. **XML 到对象**:使用 `fromXML()` 方法将 XML 字符串反序列化回 Java 对象。 ```java YourObject obj = (YourObject) xstream.fromXML(xml); ``` 5. **JSON 支持**:通过 Jettison 库,XStream 也支持 JSON ...
- Jackson库的`ObjectMapper`类可以将Java对象直接序列化为JSON字符串,或者反序列化JSON字符串为Java对象。 - Gson库同样提供了类似的`toJson`和`fromJson`方法,方便地在Java对象和JSON字符串之间转换。 4. ...
在Java中,可以使用各种库来实现XML到JSON的转换,其中XStream是一个流行的库,它提供了强大的XML序列化和反序列化功能。XStream虽然主要设计用来XML操作,但也可以通过添加额外的转换器来处理JSON。首先,你需要...