2、 将XML转换成Java对象
/*============================XML String >>>>> Java Object ==========================*/
/**
* <b>function:</b>转换xml文档到java对象
* @author hoojo
* @createDate Nov 28, 2010 3:00:27 PM
*/
@Test
public void readXML2Object() {
XMLSerializer xmlSerializer = new XMLSerializer();
fail("============== XML >>>> Java String Array ==================");
String[] sa = {"a", "b", "c"};
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(sa)));
fail(jsonArray.toString());
String[] s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[0].toString());
fail("============== XML >>>> Java boolean Array ==================");
boolean[] bo = { true, false, true };
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(bo)));
bo = (boolean[]) JSONArray.toArray(jsonArray, boolean.class);
fail(bo.toString());
System.out.println(bo[0]);
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON(bo)));
bo = (boolean[]) JSONArray.toArray(jsonArray, boolean.class);
fail(bo.toString());
System.out.println(bo[0]);
fail("==============Java Object Array >>> JSON Array ==================");
Object[] o = { 1, "a", true, 'A', sa, bo };
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(o)));
System.out.println(jsonArray.getInt(0));
System.out.println(jsonArray.get(1));
System.out.println(jsonArray.getBoolean(2));
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON(o)));
System.out.println(jsonArray.get(4));
System.out.println(jsonArray.getJSONArray(5).get(0));
System.out.println(jsonArray.get(5));
fail("==============Java String >>> JSON ==================");
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject("['json','is','easy']")).toString());
s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[0].toString());
jsonObject = (JSONObject) xmlSerializer.read(xmlSerializer.write(JSONObject.fromObject("{'json':'is easy'}")).toString());
Object obj = JSONObject.toBean(jsonObject);
System.out.println(obj);
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON("['json','is','easy']")).toString());
s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[1].toString());
}
/**
* <b>function:</b>转换xml文档到java对象
* @author hoojo
* @createDate Nov 28, 2010 3:00:27 PM
*/
@Test
public void readXML2Object() {
XMLSerializer xmlSerializer = new XMLSerializer();
fail("============== XML >>>> Java String Array ==================");
String[] sa = {"a", "b", "c"};
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(sa)));
fail(jsonArray.toString());
String[] s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[0].toString());
fail("============== XML >>>> Java boolean Array ==================");
boolean[] bo = { true, false, true };
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(bo)));
bo = (boolean[]) JSONArray.toArray(jsonArray, boolean.class);
fail(bo.toString());
System.out.println(bo[0]);
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON(bo)));
bo = (boolean[]) JSONArray.toArray(jsonArray, boolean.class);
fail(bo.toString());
System.out.println(bo[0]);
fail("==============Java Object Array >>> JSON Array ==================");
Object[] o = { 1, "a", true, 'A', sa, bo };
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject(o)));
System.out.println(jsonArray.getInt(0));
System.out.println(jsonArray.get(1));
System.out.println(jsonArray.getBoolean(2));
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON(o)));
System.out.println(jsonArray.get(4));
System.out.println(jsonArray.getJSONArray(5).get(0));
System.out.println(jsonArray.get(5));
fail("==============Java String >>> JSON ==================");
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONArray.fromObject("['json','is','easy']")).toString());
s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[0].toString());
jsonObject = (JSONObject) xmlSerializer.read(xmlSerializer.write(JSONObject.fromObject("{'json':'is easy'}")).toString());
Object obj = JSONObject.toBean(jsonObject);
System.out.println(obj);
jsonArray = (JSONArray) xmlSerializer.read(xmlSerializer.write(JSONSerializer.toJSON("['json','is','easy']")).toString());
s = (String[]) JSONArray.toArray(jsonArray, String.class);
fail(s[1].toString());
}
主要运用到XMLSerializer的read方法,将xml内容读取后,转换成Java对象。运行后结果如下:
============== XML >>>> Java String Array ==================
["a","b","c"]
a
============== XML >>>> Java boolean Array ==================
[Z@15856a5
true
[Z@79ed7f
true
==============Java Object Array >>> JSON Array ==================
1
a
true
["a","b","c"]
true
["true","false","true"]
==============Java String >>> JSON ==================
json
net.sf.ezmorph.bean.MorphDynaBean@c09554[
{json=is easy}
]
is
["a","b","c"]
a
============== XML >>>> Java boolean Array ==================
[Z@15856a5
true
[Z@79ed7f
true
==============Java Object Array >>> JSON Array ==================
1
a
true
["a","b","c"]
true
["true","false","true"]
==============Java String >>> JSON ==================
json
net.sf.ezmorph.bean.MorphDynaBean@c09554[
{json=is easy}
]
is