XML解析转JSON

VIP Stone 2015-06-11 11:09:06
<?xml version="1.0" encoding="UTF-8"?>
<checkOrderResponse
performedDatetime="1900-05-04T20:38:01.000+04:00"
code="100"
invoiceId="1234567"
shopId="13"
message="The phone number specified does not exist"
techMessage="Invalid phone number"/>

XML解析成json
{performedDatetime:"1900-05-04T20:38:01.000+04:00",code:"100",invoiceId:"1234567",shopId:"13",message:"abc"}

求java代码或者工具类,在线等

...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
VIP Stone 2015-06-11
  • 打赏
  • 举报
回复
StringBuffer str = new StringBuffer(); str.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); str.append(" <checkOrderResponse "); str.append(" performedDatetime=\"1900-05-04T20:38:01.000+04:00\""); str.append(" code=\"0\" invoiceId=\"1234567\" "); str.append(" shopId=\"13\"/>"); // 创建一个Document对象,将上面xml格式字符串转成Document Document document; Element root = null; try { document = DocumentHelper.parseText(str.toString()); root = document.getRootElement(); } catch (DocumentException e) { e.printStackTrace(); } // 获取节点 // 获取节点的属性 String performedDatetime = root.attribute("performedDatetime").getText(); String code = root.attribute("code").getText(); String invoiceId = root.attribute("invoiceId").getText(); String shopId = root.attribute("shopId").getText(); // 创建json对象 JSONObject json = new JSONObject(); json.put("performedDatetime", performedDatetime); json.put("code", code); json.put("invoiceId", invoiceId); json.put("shopId", shopId); System.out.println(json); 效果出来了
oh_Maxy 版主 2015-06-11
  • 打赏
  • 举报
回复
本来想可以设计一个对象,然后通过xml解析工具(com.thoughtworks.xstream.XStream)将xml解析成对象,然后将对象转为json(Jackson或者Gson等)。 但是你的xml好特别,都用属性来存值的,而不是节点。。
CoderNiuRouGan 2015-06-11
  • 打赏
  • 举报
回复
引用 1 楼 qq_27498153 的回复:

/*
<?xml version="1.0" encoding="UTF-8"?>
<root a="a" b="b" c="c" d="d"/>
*/

// 创建一个Document对象,将上面xml格式字符串转成Document
Document document = DocumentHelper.parseText(String str);
// 获取节点
Element root = document.getRootElement();
// 获取节点的属性
String a = root.attribute("a");
String b = root.attribute("b");
String c = root.attribute("c");
String d = root.attribute("d");
// 创建json对象
JSONObject json = new JSONObject();
json.put("a", a);
json.put("b", b);
json.put("c", c);
json.put("d", d);
以上代码没经过实际测试,只是给出一个大体写法,其中xml的操作使用的是DOM4J
String a = root.attribute("a");改为String a = root.attribute("a").getText();
CoderNiuRouGan 2015-06-11
  • 打赏
  • 举报
回复

/*
<?xml version="1.0" encoding="UTF-8"?>
<root a="a" b="b" c="c" d="d"/>
*/

// 创建一个Document对象,将上面xml格式字符串转成Document
Document document = DocumentHelper.parseText(String str);
// 获取节点
Element root = document.getRootElement();
// 获取节点的属性
String a = root.attribute("a");
String b = root.attribute("b");
String c = root.attribute("c");
String d = root.attribute("d");
// 创建json对象
JSONObject json = new JSONObject();
json.put("a", a);
json.put("b", b);
json.put("c", c);
json.put("d", d);
以上代码没经过实际测试,只是给出一个大体写法,其中xml的操作使用的是DOM4J

50,348

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧