XML/JSON的类库,Xstream!
XML/JSON的操作类库,
codehaus的xstream,很简单的在xml/json和java对象间转换。
1、所需jar包:
xstream-[version].jar
和
xpp3-[version].jar
。xpp3不是必须的,也可以用标准的JAXP DOM parser 来代替,如:
XStream xstream
=
new
XStream(
new
DomDriver());
//
does not require XPP3 library
另外,通过 alias可实现xml属性的支持。更多功能,请参阅 Tutorial 。
2、XML转换代码片段:
Class 转换为 xml 的代码片段:
XStream xstream = new XStream();
joe.setPhone( new PhoneNumber( 123 , " 1234-456 " ));
joe.setFax( new PhoneNumber( 123 , " 9999-999 " ));
String xml = xstream.toXML(joe);
xstream.alias("person", Person.class);Person joe = new Person( " Joe " , " Walnes " );
xstream.alias("phonenumber", PhoneNumber.class);
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:
<
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 >
< 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类的代码片段:
Person newJoe
=
(Person)xstream.fromXML(xml);
3、JSON
另需jar包:jettison.jar 、stax-api.jar。JSON Turorial 。
Write to json:
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));
}
}
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:
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());
}
}
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!