xml TO json

把xml文件转为json

采用的dom4j读取xml文档,需导入以下jar包

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.35</version>
</dependency>

方法一使用了json-lib的工具包,需要在maven中导入以下jar包
方法二使用了递归的方式

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>
<dependency>
    <groupId>xom</groupId>
    <artifactId>xom</artifactId>
    <version>1.2.5</version>
</dependency>

完整代码:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * Created by 东方电视台 on 2017/7/26.
 */
public class xmlTojson {
    public static void main(String[] args) throws Exception{
        JSONObject result = getXml2("test.xml");
        System.out.println(result.toString());
    }
    //方法一 利用json-lib工具包解析 
    public static JSONObject getXml(String filename){
        XMLSerializer xmlSerializer = new XMLSerializer();
        String jsonStr = xmlSerializer.readFromFile(new File(filename)).toString();
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        return jsonObj;
    }
    //方法二 递归解析
    public static JSONObject getXml2(String filename) throws Exception{
        JSONObject jsonObj = new JSONObject();
        Document doc = readXml(filename);
        getJson(doc.getRootElement(), jsonObj);
        return jsonObj;
    }
    public static Document readXml(String filename) throws DocumentException {
        Document document = null;
        try{
            //获取xml文件
            File file = new File(filename);
            //创建SAXReader对象
            SAXReader reader = new SAXReader();
            //读取文件
            document = reader.read(file);
        }catch (DocumentException e){
            e.printStackTrace();
        }
        return  document;
    }
    public static boolean isEmpty(String s){
        if(StringUtils.isBlank(s) || "null".equals(s) || s.length() == 0){
            return true;
        }
        return false;
    }
    public static void getJson(Element element,JSONObject json){
        //如果是属性
        for(Object attrObj : element.attributes()){
            Attribute attr = (Attribute)attrObj;
            if(!isEmpty(attr.getValue())){
                json.put("@"+attr.getName(), attr.getValue());
            }
        }
        List<Element> childList = element.elements();
        //如果没有子元素,只有一个值
        if(childList.isEmpty() && !isEmpty(element.getText())){
            json.put(element.getName(), element.getText());
        }
        for(Element e : childList){//有子元素
            if(!e.elements().isEmpty()){//子元素也有子元素
                JSONObject childJson = new JSONObject();
                getJson(e,childJson);
                Object o = json.get(e.getName());
                if(o != null){
                    JSONArray jsona = null;
                    //如果此元素已存在,则转为jsonArray
                    if(o instanceof JSONObject){
                        JSONObject jsono = (JSONObject)o;
                        json.remove(e.getName());
                        jsona = new JSONArray();
                        jsona.add(jsono);
                        jsona.add(childJson);
                    }
                    if(o instanceof JSONArray){
                        jsona = (JSONArray)o;
                        jsona.add(childJson);
                    }
                    json.put(e.getName(), jsona);
                }else{
                    if(!childJson.isEmpty()){
                        json.put(e.getName(), childJson);
                    }
                }
            }else{
                //子元素没有子元素
                for(Object o:element.attributes()){
                    Attribute attr = (Attribute)o;
                    if(!isEmpty(attr.getValue())){
                        json.put("@"+attr.getName(), attr.getValue());
                    }
                }
//                if(!isEmpty(e.getText())){
                    json.put(e.getName(), e.getText());
//                }
            }
        }
    }
}

参照博客
1: http://blog.csdn.net/tkggetg/article/details/47784321

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值