XML/JSON的类库,Xstream!

XML/JSON的操作类库,codehaus的xstream,很简单的在xml/json和java对象间转换。

1、所需jar包:xstream-[version].jarxpp3-[version].jar 。xpp3不是必须的,也可以用标准的JAXP DOM parser 来代替,如:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> XStream xstream  =   new  XStream( new  DomDriver());  //  does not require XPP3 library


另外,通过alias可实现xml属性的支持。更多功能,请参阅 Tutorial

2、XML转换代码片段:
Class 转换为 xml 的代码片段:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->XStream xstream = new XStream();
xstream.alias("person", Person.class);

xstream.alias("phonenumber", PhoneNumber.class);
Person joe  =   new  Person( " Joe " " Walnes " );
joe.setPhone(
new  PhoneNumber( 123 " 1234-456 " ));
joe.setFax(
new  PhoneNumber( 123 " 9999-999 " ));
String xml 
=  xstream.toXML(joe);


The resulting XML looks like this:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < person >
  
< firstname > Joe </ firstname >
  
< lastname > Walnes </ lastname >
  
< phone >
    
< code > 123 </ code >
    
< number > 1234-456 </ number >
  
</ phone >
  
< fax >
    
< code > 123 </ code >
    
< number > 9999-999 </ number >
  
</ fax >
</ person >


xml转换为java类的代码片段:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> Person newJoe  =  (Person)xstream.fromXML(xml);


3、JSON

另需jar包:jettison.jar 、stax-api.jar。JSON Turorial

Write to json:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.sensatic.json.test;

import  com.thoughtworks.xstream.XStream;
import  com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public   class  WriteTest {
    
public   static   void  main(String[] args) {
        Product product 
=   new  Product( " Banana " " 123 " 23.00 );
        XStream xstream 
=   new  XStream( new  JettisonMappedXmlDriver());
        xstream.alias(
" product " , Product. class );
        System.out.println(xstream.toXML(product));    
    }
}


Read from json:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  org.sensatic.json.test;

import  com.thoughtworks.xstream.XStream;
import  com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public   class  ReadTest {
    
public   static   void  main(String[] args) {
        String json 
=   " {\ " product\ " :{\ " name\ " :\ " Banana\ " ,\ " id\ " :\ " 123 \ ""
            
+   " ,\ " price\ " :\ " 23.0 \ " }} " ;
            
        XStream xstream 
=   new  XStream( new  JettisonMappedXmlDriver());
        xstream.alias(
" product " , Product. class );
        Product product 
=  (Product)xstream.fromXML(json);
        System.out.println(product.getName());
    }
}


And that's how simple XStream is!

from:http://www.blogjava.net/josson/archive/2008/01/15/175381.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值