将String格式XML转换成JSON

来源:互联网 发布:淘宝偷图会有什么后果 编辑:程序博客网 时间:2024/05/21 07:02

导入包:json.rar

1.全部转换

String xml = "<Document TaskGuid='c3d791d2-8a92-44d6-aa64-ad67cd08c0cd' DataGuid='' DataType='QueryStandNoteGUID'>"                + "<StandInfo Name='站台名称' NoteGuid='站台编号' Canton='所在行政区' Road='所在道路' Sect='所在路段' Direct='站点方位'/>"                   +"<StandInfo Name='站台名称' NoteGuid='站台编号' Canton='所在行政区' Road='所在道路' Sect='所在路段' Direct='站点方位'/></Document>";    JSONObject a= XML.toJSONObject(xml);

———————————-结束————————————-
2.将xml字符串去掉头:使用DOM4J
“<\?xml version=”1.0” encoding=”UTF-8”\?>”

 Document document=DocumentHelper.parseText(xml2); System.out.println(document.getRootElement().asXML());

3.读取JSON中指定字段值:

System.out.print(a.getJSONObject("Document").getString("StandInfo"));
1 0