package com.test;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
* 2018.02.05
* @author tlimited
*
*/
public class Json2XML {
public static void main(String[] args) throws DocumentException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "test");
jsonObject.put("age", "28");
System.out.println(json2Xml(jsonObject, "root"));
}
/**
* json转xml
* @param json
* @param rootName
* @return
* @throws DocumentException
*/
public static String json2Xml(JSONObject json, String rootName) throws DocumentException {
String xml = "";
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false);
xmlSerializer.setRootName(rootName);
String sContent = xmlSerializer.write(json);
try {
Document docCon = DocumentHelper.parseText(sContent);
xml = docCon.getRootElement().asXML();
} catch (DocumentException e) {
e.printStackTrace();
}
return xml;
}
/**
* 这个方法有bug,读取时候会把头弄没了
* xml转json
* @param json
* @return
* @throws DocumentException
*/
public static String xml2Json(String json) throws DocumentException {
String sXml = "";
XMLSerializer xmlSerializer = new XMLSerializer();
String sContent = xmlSerializer.read(json).toString();
System.out.println(sContent);
return null;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Testsl4j.zip (37个子文件)
Testsl4j
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 654B
lib
ezmorph-1.0.6.jar 84KB
org.json-2.0.jar 48KB
dom4j-2.1.0.jar 315KB
commons-beanutils-1.8.0.jar 226KB
xom-1.2.5.jar 302KB
fastjson-1.2.6.jar 411KB
logback-classic-1.1.6.jar 285KB
commons-collections-3.2.2.jar 575KB
json-lib-2.4-jdk15-sources.jar 103KB
json-lib-2.3-jdk15.jar 148KB
commons-lang-2.4.jar 256KB
commons-logging-1.2.jar 60KB
slf4j-api-1.7.5.jar 25KB
logback-core-1.1.6.jar 450KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 476B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 567B
org.eclipse.jdt.core.prefs 364B
src
logback.xml 4KB
com
test2
User.java 454B
Test.java 615B
test
TestJson.java 701B
TestList.java 595B
Json2XML.java 2KB
.project 1KB
.classpath 919B
logs.dir_IS_UNDEFINED
logback-test.log 600B
build
classes
logback.xml 4KB
com
test2
Test.class 1KB
User.class 942B
test
Json2XML.class 2KB
TestList.class 764B
TestJson.class 899B
共 37 条
- 1
资源评论
- 莫名32019-03-25浪费了 很不好
Tlimited
- 粉丝: 174
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功