博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中json转xml
阅读量:6429 次
发布时间:2019-06-23

本文共 6073 字,大约阅读时间需要 20 分钟。

参考:http://heipark.iteye.com/blog/1394844

需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下:

xml代码

net.sf.json-lib
json-lib
2.1
jdk15
xom
xom
1.2.5
jar
compile

java代码

package com.nirvanainfo.salesleads.framework.utils; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSON; import net.sf.json.JSONSerializer; import net.sf.json.xml.XMLSerializer; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /**  * Created by Administrator on 2016/9/2.  */ public class XmlConverUtil {
/** * map to xml xml
value1
value2
......
* * @param map * @return */ public static String maptoXml(Map map) {
Document document = DocumentHelper.createDocument(); Element nodeElement = document.addElement("node"); for (Object obj : map.keySet()) {
Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(obj)); keyElement.setText(String.valueOf(map.get(obj))); } return doc2String(document); } /** * list to xml xml
value1
value2
......
value1
value2
......
* * @param list * @return */ public static String listtoXml(List list) throws Exception {
Document document = DocumentHelper.createDocument(); Element nodesElement = document.addElement("nodes"); int i = 0; for (Object o : list) {
Element nodeElement = nodesElement.addElement("node"); if (o instanceof Map) {
for (Object obj : ((Map) o).keySet()) {
Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(obj)); keyElement.setText(String.valueOf(((Map) o).get(obj))); } } else {
Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(i)); keyElement.setText(String.valueOf(o)); } i++; } return doc2String(document); } /** * json to xml {"node":{"key":{"@label":"key1","#text":"value1"}}} conver *
value1
* * @param json * @return */ public static String jsontoXml(String json) {
try {
XMLSerializer serializer = new XMLSerializer(); JSON jsonObject = JSONSerializer.toJSON(json); return serializer.write(jsonObject); } catch (Exception e) {
e.printStackTrace(); } return null; } /** * xml to map xml
value1
value2
......
* * @param xml * @return */ public static Map xmltoMap(String xml) {
try {
Map map = new HashMap(); Document document = DocumentHelper.parseText(xml); Element nodeElement = document.getRootElement(); List node = nodeElement.elements(); for (Iterator it = node.iterator(); it.hasNext();) {
Element elm = (Element) it.next(); map.put(elm.attributeValue("label"), elm.getText()); elm = null; } node = null; nodeElement = null; document = null; return map; } catch (Exception e) {
e.printStackTrace(); } return null; } /** * xml to list xml
value1
value2
......
value1
value2
......
* * @param xml * @return */ public static List xmltoList(String xml) {
try {
List list = new ArrayList(); Document document = DocumentHelper.parseText(xml); Element nodesElement = document.getRootElement(); List nodes = nodesElement.elements(); for (Iterator its = nodes.iterator(); its.hasNext();) {
Element nodeElement = (Element) its.next(); Map map = xmltoMap(nodeElement.asXML()); list.add(map); map = null; } nodes = null; nodesElement = null; document = null; return list; } catch (Exception e) {
e.printStackTrace(); } return null; } /** * xml to json
value1
转化为 * {"key":{"@label":"key1","#text":"value1"}} * * @param xml * @return */ public static String xmltoJson(String xml) {
XMLSerializer xmlSerializer = new XMLSerializer(); return xmlSerializer.read(xml).toString(); } /** * * @param document * @return */ public static String doc2String(Document document) {
String s = ""; try {
// 使用输出流来进行转化 ByteArrayOutputStream out = new ByteArrayOutputStream(); // 使用UTF-8编码 OutputFormat format = new OutputFormat(" ", true, "UTF-8"); XMLWriter writer = new XMLWriter(out, format); writer.write(document); s = out.toString("UTF-8"); } catch (Exception ex) {
ex.printStackTrace(); } return s; } }

 

转载于:https://www.cnblogs.com/qyhol/p/5833213.html

你可能感兴趣的文章
Extra:Variable Types
查看>>
js传参时,没有参数传入,默认值的设置
查看>>
ASP.NET温故而知新学习系列之ASP.NET多线程编程—.NET下的多线程编程Thread中委托的使用(六)...
查看>>
使用 Spring HATEOAS 开发 REST 服务
查看>>
最新整理知识结构图
查看>>
linux安装mysql
查看>>
flask 2 进阶
查看>>
JS 循环遍历JSON数据
查看>>
sentences in movies and teleplays[1]
查看>>
【20181023T1】战争【反向并查集】
查看>>
win7网络共享原来如此简单,WiFi共享精灵开启半天都弱爆了!
查看>>
iOS9 未受信任的企业级开发者
查看>>
paper 40 :鲁棒性robust
查看>>
十五、查找函数——找到目标对象。串联函数——操作对象链
查看>>
做一个会使用PS的前端开发
查看>>
优化MySchool数据库(事务、视图、索引)
查看>>
硬件开发之pcb---PCB抗干扰设计原则
查看>>
关于字符串循环遍历的两种方法
查看>>
使用笔记:TF辅助工具--tensorflow slim(TF-Slim)
查看>>
CCF-NOIP-2018 提高组(复赛) 模拟试题(一)
查看>>