技术开发 频道

JSON lib框架转换JSON XML不再困难!

  四、 JSON-lib对XML的支持

  1、 将Java对象到XML

/*============================Java Object >>>>> XML ==========================*/
/**
* <b>function:</b> 转换Java对象到XML
* 需要额外的jar包:xom.jar
*
@author hoojo
* @createDate Nov 28, 2010 2:39:55 PM
*/
@Test
public void writeObject2XML() {
XMLSerializer xmlSerializer
= new XMLSerializer();
fail(
"==============Java String Array >>> XML ==================");
//xmlSerializer.setElementName("bean");
fail(xmlSerializer.write(JSONArray.fromObject(bean)));
String[] sa
= {"a", "b", "c"};
fail(
"==============Java String Array >>> XML ==================");
fail(xmlSerializer.write(JSONArray.fromObject(sa)));
fail(
"==============Java boolean Array >>> XML ==================");
boolean[] bo = { true, false, true };
fail(xmlSerializer.write(JSONArray.fromObject(bo)));
fail(xmlSerializer.write(JSONSerializer.toJSON(bo)));
Object[] o
= { 1, "a", true, 'A', sa, bo };
fail(
"==============Java Object Array >>> JSON Array ==================");
fail(xmlSerializer.write(JSONArray.fromObject(o)));
fail(xmlSerializer.write(JSONSerializer.toJSON(o)));
fail(
"==============Java String >>> JSON ==================");
fail(xmlSerializer.write(JSONArray.fromObject(
"['json','is','easy']")).toString());
fail(xmlSerializer.write(JSONObject.fromObject(
"{'json':'is easy'}")).toString());
fail(xmlSerializer.write(JSONSerializer.toJSON(
"['json','is','easy']")).toString());
}

 

  主要运用的是XMLSerializer的write方法,这个方法可以完成java对象到xml的转换,不过你很容易就可以看到这个xml序列化对象,需要先将java对象转成json对象,然后再将json转换吃xml文档。

  运行后结果如下:

==============Java String Array >>> XML ==================
<?xml version="1.0" encoding="UTF-8"?>
<a><e class="object">
<address type="string">address</address><birthday class="object"><birthday type="string">2010-11-22</birthday></birthday>
<email type="string">email</email><id type="number">1</id><name type="string">haha</name>
</e></a>

==============Java String Array >>> XML ==================
<?xml version="1.0" encoding="UTF-8"?>
<a><e type="string">a</e><e type="string">b</e><e type="string">c</e></a>

==============Java boolean Array >>> XML ==================
<?xml version="1.0" encoding="UTF-8"?>
<a><e type="boolean">true</e><e type="boolean">false</e><e type="boolean">true</e></a>

<?xml version="1.0" encoding="UTF-8"?>
<a><e type="boolean">true</e><e type="boolean">false</e><e type="boolean">true</e></a>

==============Java Object Array >>> JSON Array ==================
<?xml version="1.0" encoding="UTF-8"?>
<a><e type="number">1</e><e type="string">a</e><e type="boolean">true</e><e type="string">A</e><e class="array">
<e type="string">a</e><e type="string">b</e><e type="string">c</e></e><e class="array"><e type="boolean">true</e>
<e type="boolean">false</e><e type="boolean">true</e></e></a>

<?xml version="1.0" encoding="UTF-8"?>
<a><e type="number">1</e><e type="string">a</e><e type="boolean">true</e><e type="string">A</e><e class="array">
<e type="string">a</e><e type="string">b</e><e type="string">c</e></e><e class="array"><e type="boolean">true</e>
<e type="boolean">false</e><e type="boolean">true</e></e></a>

==============Java String >>> JSON ==================
<?xml version="1.0" encoding="UTF-8"?>
<a><e type="string">json</e><e type="string">is</e><e type="string">easy</e></a>

<?xml version="1.0" encoding="UTF-8"?>
<o><json type="string">is easy</json></o>

<?xml version="1.0" encoding="UTF-8"?>
<a><e type="string">json</e><e type="string">is</e><e type="string">easy</e></a>

 

  上面的节点名称有点乱,你可以通过setElementName设置节点名称。

0
相关文章