java将XML文档转换成json格式数据的示例
程序员文章站
2023-12-04 11:54:22
本文介绍了java将xml文档转换成json格式数据的示例,分享给大家,具体如下:
功能
将xml文档转换成json格式数据
说明
依赖包:...
本文介绍了java将xml文档转换成json格式数据的示例,分享给大家,具体如下:
功能
将xml文档转换成json格式数据
说明
依赖包:
1. jdom-2.0.2.jar : xml解析工具包;
2. fastjson-1.1.36.jar : 阿里巴巴研发的高性能json工具包
程序源代码
package com.xxx.open.pay.util; import com.alibaba.fastjson.jsonobject; import org.jdom2.element; import org.jdom2.jdomexception; import org.jdom2.input.saxbuilder; import java.io.bytearrayinputstream; import java.io.ioexception; import java.io.inputstream; import java.util.linkedlist; import java.util.list; /** * created by 1250052380@qq.com on 2015/5/19. */ public class xmlutil { public static jsonobject xml2json(byte[] xml) throws jdomexception, ioexception { jsonobject json = new jsonobject(); inputstream is = new bytearrayinputstream(xml); saxbuilder sb = new saxbuilder(); org.jdom2.document doc = sb.build(is); element root = doc.getrootelement(); json.put(root.getname(), iterateelement(root)); return json; } private static jsonobject iterateelement(element element) { list node = element.getchildren(); element et = null; jsonobject obj = new jsonobject(); list list = null; for (int i = 0; i < node.size(); i++) { list = new linkedlist(); et = (element) node.get(i); if (et.gettexttrim().equals("")) { if (et.getchildren().size() == 0) continue; if (obj.containskey(et.getname())) { list = (list) obj.get(et.getname()); } list.add(iterateelement(et)); obj.put(et.getname(), list); } else { if (obj.containskey(et.getname())) { list = (list) obj.get(et.getname()); } list.add(et.gettexttrim()); obj.put(et.getname(), list); } } return obj; } public static void main(string[] args) throws jdomexception, ioexception { string xml="<?xml version=\"1.0\" encoding=\"utf-8\" ?><mobaoaccount messagetype=\"usermobilepay\" platformid=\"b2ctest\"><orderno>m20150521084825</orderno><tradeamt>5000.00</tradeamt><commission>0.5</commission><userid>zhuxiaolong</userid><merchid>zhuxiaolong1</merchid><tradetype>0</tradetype><custparam>123</custparam> <notifyurl>http://mobaopay.com/callback.do</notifyurl><tradesummary>订单</tradesummary></mobaoaccount>"; jsonobject json=xml2json(xml.getbytes()); system.out.println(json.tojsonstring()); } }
执行结果
xml原文:
<?xml version="1.0" encoding="utf-8" ?> <mobaoaccount messagetype="usermobilepay" platformid="b2ctest"> <orderno>m20150521084825</orderno> <tradeamt>5000.00</tradeamt> <commission>0.5</commission> <userid>xiaolong</userid> <merchid>xiaolong1</merchid> <tradetype>0</tradetype> <custparam>123</custparam> <notifyurl>http://mobaopay.com/callback.do</notifyurl> <tradesummary>订单</tradesummary> </mobaoaccount>
转换后的json格式数据
{ "mobaoaccount": { "commission": [ "0.5" ], "custparam": [ "123" ], "merchid": [ "zhuxiaolong1" ], "notifyurl": [ "http://mobaopay.com/callback.do" ], "orderno": [ "m20150521084825" ], "tradeamt": [ "5000.00" ], "tradesummary": [ "订单" ], "userid": [ "zhuxiaolong" ], "tradetype": [ "0" ] } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Spring之WEB模块配置详解