1.XStream Tool Description:
This tool, the jar package for this tool, can help us to generate XML-formatted data such as JavaBean, set (LIST,SET,MAP), etc.
Jar Package:
Xstream-1.4.4.jar, this is the core package.
Xpp3_min-1.1.4c.jar This is a must-depend package
As long as importing these two jar packages into the build path, you can use the.
2.json-lib Introduction:
This jar can help us to generate JSON-formatted data such as JavaBean, set (LIST,SET,MAP), etc.
Jar Package:
This is the core package.
This is the core package's dependency package
For convenience, all of the following cases are generated on the basis of the province class and the City class:
(1). Povince class
Package App.xstream;import Java.util.list;public class Province {private int id;private String name;private list<city > Cities;public Province (int id, String name, list<city> cities) {super (); this.id = Id;this.name = Name;this.citi es = cities;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public list<city> GetCities () {return cities;} public void Setcities (list<city> cities) {this.cities = cities;}}
(2). City class
Package App.xstream;public class City {private int id;private string name;public city (int ID, string string) {super (); . id = Id;this.name = string;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Case 1: Demonstrating the generation of XML format data:
Xstream.useattributefor (Province.class, "id"); This sentence means to take the ID as a property of province.
Xstream.alias ("province", Province.class), which means to convert <app.province> into <province> format
Package App.xstream;import Java.util.arraylist;import Java.util.list;import com.thoughtworks.xstream.XStream; public class Xstreamdemo {public static void main (string[] args) {City c1=new City (1, "Xingtai"); City C2=new City (2, "Tangshan"); City C3=new City (3, "Shijiazhuang"); List<city> cities=new arraylist<city> (); Cities.add (C1); Cities.add (C2); Cities.add (C3); Province P=new Province (1, "Hebei province", cities);//Create Instance XStream xstream=new XStream (); Xstream.useattributefor (Province.class , "id"),/xstream.useattributefor (Province.class, "name"), Xstream.useattributefor (City.class, "id"); Xstream.useattributefor (City.class, "name"), Xstream.alias ("province", Province.class); Xstream.alias ("City", City.class);//Convert object to XML format string province = Xstream.toxml (p); System.out.println (province);}}
Case 2: Demo to generate JSON-formatted data
Here I wrote a tool class for transforming data.
Package App.xstream;import Java.util.list;import Java.util.map;import net.sf.json.jsonarray;import Net.sf.json.jsonobject;import Net.sf.json.jsonconfig;import Net.sf.json.util.cycledetectionstrategy;import net.sf.json.xml.xmlserializer;/** * Tool class for processing JSON data format * * @author fellow * @Date 2013-3-31 * @version 1.0 */public Class Jsonut Il {/** * converts an array to a String-type JSON data format * * @param objects * @return */public static String Array2json (object[] objects) {Jsonar Ray Jsonarray = Jsonarray.fromobject (objects); return jsonarray.tostring ();} /** * Convert list collection to JSON data format of String type * * @param list * @return */public static String List2json (List list) {Jsonarray Jsonarr ay = jsonarray.fromobject (list); return jsonarray.tostring ();} /** * Convert the Map collection to a JSON data format of String type * * @param map * @return */public static String Map2json (map map) {Jsonobject Jsonobject = Jsonobject.fromobject (map); return jsonobject.tostring ();} /** * Converts an Object object into a JSON data format of String type * * @param object * @return */public static string Object2json (Object ObjeCT) {Jsonobject jsonobject = Jsonobject.fromobject (object); return jsonobject.tostring ();} /** * Convert XML data format to String type JSON data format * * @param XML * @return */public static string Xml2json (String xml) {Jsonarray Jsonarr ay = (jsonarray) new XMLSerializer (). read (XML); return jsonarray.tostring ();} /** * Remove fields that you do not want to generate (particularly suitable for removing cascading objects) * * @param excludes * @return */public static jsonconfig Configjson (string[] excludes) {Js OnConfig jsonconfig = new Jsonconfig (); Jsonconfig.setexcludes (excludes); Jsonconfig.setignoredefaultexcludes (true); Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient); return jsonconfig;}}
================================================================================================
Package App.xstream;import Java.util.arraylist;import Java.util.list;public class Jsondemo {public static void main ( String[] args) {City c1=new City (1, "Xingtai"); City C2=new City (2, "Tangshan"); City C3=new City (3, "Shijiazhuang"); List<city> cities=new arraylist<city> (); Cities.add (C1); Cities.add (C2); Cities.add (C3); Province P=new Province (1, "Hebei province", cities); String Object2json = Jsonutil.object2json (p); System.out.println (Object2json.tostring ());}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse DAY17 XStream generate XML Data &json-lib generate JSON data