xml字符串转json字符串

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.alibaba.fastjson.JSON;

public class Test {
    
    public static void main(String[] args) throws Exception {

        String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
        "<Batch Name=\"0901a21f1\" OcrStatus=\"10\" DocCount=\"1\" WriteTime=\"2018-02-24 19:01:46\" CompTime=\"2018-02-24T19:01:46.5307178+08:00\">"+
          "<Doc ObjectId=\"0901a21f1-0\" Name=\"1.jpg\" FormObjectId=\"59298701b9533f1934ee9581\" FormId=\"Form_5\" Form=\"身份证正面\" MainAttachFlag=\"1\" AttachStamp=\"0\">"+
            "<Field FieldId=\"1\" Name=\"姓名\" Rect=\"181, 48, 276, 77\" IsSusp=\"true,false\">xx</Field>"+
            "<Field FieldId=\"7\" Name=\"住址\" Rect=\"179, 305, 464, 191\" IsSusp=\"false,true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false\">安徽省池州市xxx"+
        "</Field>"+
            "<Field FieldId=\"8\" Name=\"公民身份证号码\" Rect=\"335, 515, 642, 95\" IsSusp=\"false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false\">342901199xxx</Field>"+
            "<Field FieldId=\"3\" Name=\"民族\" Rect=\"395, 135, 121, 77\" IsSusp=\"false\">汉</Field>"+
            "<Field FieldId=\"2\" Name=\"性别\" Rect=\"180, 142, 84, 70\" IsSusp=\"true\">男</Field>"+
            "<Field FieldId=\"4\" Name=\"年\" Rect=\"183, 228, 110, 59\" IsSusp=\"true,true,false,false\">1994</Field>"+
           "<Field FieldId=\"5\" Name=\"月\" Rect=\"345, 227, 61, 63\" IsSusp=\"false\">9</Field>"+
          "<Field FieldId=\"6\" Name=\"日\" Rect=\"443, 225, 58, 65\" IsSusp=\"false\">7</Field>"+
         "</Doc>"+
        "</Batch>";
        System.out.println(xmlStrToJsonStr(xmlStr));
    }
    private static String xmlStrToJsonStr(String xmlStr) throws Exception{

        Map<String ,String> map = new HashMap<String, String>();
        String jsonStr = null;
        StringReader sr = new StringReader(xmlStr);
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=factory.newDocumentBuilder();
        Document document = builder.parse(is);

        NodeList batchNodeList = document.getElementsByTagName("Batch");
        for(int i=0;i<batchNodeList.getLength();i++){
            Node batchNode = batchNodeList.item(i);

            NodeList docNodeList = batchNode.getChildNodes();
            for(int j = 0;j < docNodeList.getLength();j++){
                Node docNode = docNodeList.item(j);
                if (docNode.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap docAttrs = docNode.getAttributes();
                    for (int k = 0; k < docAttrs.getLength(); k++) {
                        Node attr = docAttrs.item(k);
                        if("Form".equals(attr.getNodeName())){
                            map.put("正反面","身份证正面");
                        }
                    }

                    NodeList fieldNodeList = docNode.getChildNodes();
                    for (int k = 0; k < fieldNodeList.getLength(); k++) {
                        Node fieldNode = fieldNodeList.item(k);
                        if (fieldNode.getNodeType() == Node.ELEMENT_NODE) {
                            NamedNodeMap fieldNodeAttrs = fieldNode.getAttributes();
                            for (int l = 0; l < fieldNodeAttrs.getLength(); l++) {
                                Node attr = fieldNodeAttrs.item(l);
                                if("姓名".equals(attr.getNodeValue())){
                                    map.put("姓名",fieldNode.getTextContent());
                                }else if("住址".equals(attr.getNodeValue())){
                                    map.put("住址",fieldNode.getTextContent());
                                }else if("公民身份证号码".equals(attr.getNodeValue())){
                                    map.put("公民身份证号码",fieldNode.getTextContent());
                                }else if("民族".equals(attr.getNodeValue())){
                                    map.put("民族",fieldNode.getTextContent());
                                }else if("性别".equals(attr.getNodeValue())){
                                    map.put("性别",fieldNode.getTextContent());
                                }else if("年".equals(attr.getNodeValue())){
                                    map.put("年",fieldNode.getTextContent());
                                }else if("月".equals(attr.getNodeValue())){
                                    map.put("月",fieldNode.getTextContent());
                                }else if("日".equals(attr.getNodeValue())){
                                    map.put("日",fieldNode.getTextContent());
                                }
                            }
                        }

                    }
                }
            }
        }

        jsonStr = JSON.toJSONString(map);

        return jsonStr;
    }
    
}

猜你喜欢

转载自blog.csdn.net/h702109382/article/details/79543940