xml 형식 이 json 형식 으로 전 환 됨 --- XmlParse2JSon

3408 단어 자바
import com.alibaba.fastjson.JSONObject;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;



/**
 * The type XmlParse2Json.
 */
public class XmlParse2Json {
    public static String xml2JSON(String xml) {
        JSONObject json = new JSONObject();
        byte[] byteStream = null;
        try {
            byteStream = xml.getBytes("utf-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        InputStream is = new ByteArrayInputStream(byteStream);
        SAXBuilder sb = new SAXBuilder();
        org.jdom.Document doc = null;
        try {
            doc = sb.build(is);
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();
        json.put(root.getName(), iterateElement(root));
        String jsonString = json.toJSONString();
        return jsonString;
    }
    private static JSONObject iterateElement(Element element) {
        List node = element.getChildren();
        Element et = null;
        JSONObject obj = new JSONObject();
        //           ,      list,     
        List list = null;
        //      
        for (int i = 0; i < node.size(); i++) {
            list = new LinkedList<>();
            et = (Element) node.get(i);
            //        
            if (et.getAttributes().size() > 0) {
                //             
                List attributes = et.getAttributes();
                //         jsonobject
                JSONObject attrvalue = new JSONObject();
                //         list
                List attrvalues = new ArrayList<>();
                for (Attribute attr : attributes) {
                    //            jsonobject
                    attrvalue.put(attr.getName(), attr.getValue());
                }
                attrvalues.add(attrvalue);
                obj.put(et.getName(), attrvalues);
            }
            //        
            if (et.getTextTrim().equals("")) {
                if (et.getChildren().size() == 0)
                    continue;
                //  obj              
                if (obj.containsKey(et.getName())) {
                    //   ,     list   
                    list = (List) obj.get(et.getName());
                }
                list.add(iterateElement(et));
                obj.put(et.getName(), list);
            } else {
                if (obj.containsKey(et.getName())) {
                    list = (List) obj.get(et.getName());
                }
                list.add(et.getTextTrim());
                obj.put(et.getName(), list);
            }
        }
        return obj;
    }
    public static void main(String[] args){

        String xml = "lll1"
                + "xxx2";
        String str = xml2JSON(xml);
        System.out.println(str);
    }
}


좋은 웹페이지 즐겨찾기