无论是接口测试还是开发中,有时候会遇到接口返回回来的的数据是XML类型的,此时又不想单独写一套XML转Java Bean,因为毕竟XML作为返回结果相对来说还是没有JSON那么主流,而且以JSON字段设计的Bean对象属性不一定吻合XML,那么就需要将XML转成JSON后,再按解析JSON处理了。前段时间刚好遇到这种需求,就将实现方式分享给大家。
相关依赖
基于maven管理依赖,需要在pom文件中加入下面dependency,解析JSON用阿里的fastjson,解析xml用jdom:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>2.0.2</version>
</dependency>
转换逻辑
这里封装了一个XML2JSON工具类:
import org.apache.commons.lang.StringUtils;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import com.alibaba.fastjson.JSONObject;
import org.jdom2.Element;
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 alany on 2018/7/10.
*/
public class XmlUtils {
public static JSONObject xml2Json(String xmlStr) throws JDOMException, IOException {
if (StringUtils.isEmpty(xmlStr)) {
return null;
}
xmlStr = xmlStr.replaceAll("\\\n", "");
byte[] xml = xmlStr.getBytes("UTF-8");
JSONObject json = new JSONObject();
InputStream is = new ByteArrayInputStream(xml);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(is);
Element root = doc.getRootElement();
json.put(root.getName(), iterateElement(root));
return json;
}
private static JSONObject iterateElement(Element element) {
List<Element> node = element.getChildren();
JSONObject obj = new JSONObject();
List list = null;
for (Element child : node) {
list = new LinkedList();
String text = child.getTextTrim();
if (StringUtils.isBlank(text)) {
if (child.getChildren().size() == 0) {
continue;
}
if (obj.containsKey(child.getName())) {
list = (List) obj.get(child.getName());
}
list.add(iterateElement(child)); //遍历child的子节点
obj.put(child.getName(), list);
} else {
if (obj.containsKey(child.getName())) {
Object value = obj.get(child.getName());
try {
list = (List) value;
} catch (ClassCastException e) {
list.add(value);
}
}
if (child.getChildren().size() == 0) { //child无子节点时直接设置text
obj.put(child.getName(), text);
} else {
list.add(text);
obj.put(child.getName(), list);
}
}
}
return obj;
}
}
跑个test看下转换效果:
@Test
public void test() throws JDOMException, IOException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" +
"<sitemap>\n" +
" <loc><![CDATA[http://qq.com/xml/_1.xml]]></loc>\n" +
" <lastmod><![CDATA[2018-06-20 01:28:09]]></lastmod>\n" +
" </sitemap>\n" +
"<sitemap>\n" +
" <loc><![CDATA[http://qq.com/xml/_2.xml]]></loc>\n" +
" <lastmod><![CDATA[2018-06-20 01:28:09]]></lastmod>\n" +
" </sitemap>\n" +
"<sitemaps>\n" +
" <sitemap>\n" +
" <loc><![CDATA[http://qq.com/xml/_46.xml]]></loc>\n" +
" <lastmod><![CDATA[2018-06-20 01:28:09]]></lastmod>\n" +
" </sitemap>\n" +
" <sitemap>\n" +
" <loc><![CDATA[http://qq.com/xml/_47.xml]]></loc>\n" +
" <lastmod><![CDATA[2018-06-25 01:28:09]]></lastmod>\n" +
" </sitemap>\n" +
"</sitemaps>\n" +
"</sitemapindex>\n";
JSONObject json = xml2Json(xml);
System.out.println(json.toJSONString());
}
跑完直接生产的JSON:
{
"sitemapindex":{
"sitemaps":[
{
"sitemap":[
{
"loc":"[http://qq.com/xml/_46.xml](http://qq.com/xml/_46.xml)",
"lastmod":"2018-06-20 01:28:09"
},
{
"loc":"[http://qq.com/xml/_47.xml](http://qq.com/xml/_47.xml)",
"lastmod":"2018-06-25 01:28:09"
}
]
}
],
"sitemap":[
{
"loc":"[http://qq.com/xml/_1.xml](http://qq.com/xml/_1.xml)",
"lastmod":"2018-06-20 01:28:09"
},
{
"loc":"[http://qq.com/xml/_2.xml](http://qq.com/xml/_2.xml)",
"lastmod":"2018-06-20 01:28:09"
}
]
}
}
PS: XML格式要标准(标签要成对出现),不然可能会解析不了,测试了较复杂的HTML页面会转换失败的。
作者:测试开发栈
链接:https://www.jianshu.com/p/89b5c4924294
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。