XML格式字符串获取指定标签值

//XML格式字符串获取指定标签值
​​​​​​​public static Map<String, String> getXMLStringValue(String result) {
    Map<String, String> rm = new HashMap<String, String>();
    StringReader sr = new StringReader(result);    // result数据源:XML格式字符串
    InputSource is = new InputSource(sr);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    org.w3c.dom.Document document = null;
    try {
        document = builder.parse(is);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Element rootElement = document.getDocumentElement();
    NodeList nodes = rootElement.getChildNodes();    // 一级标签

    for (int i = 0; i < nodes.getLength(); i++) {

        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getFirstChild() != null) {
                rm.put(node.getNodeName(), node.getFirstChild().getNodeValue());
            }
            NodeList nodeList = node.getChildNodes();    // 二级标签
            if (nodeList != null) {
                for (int j = 0; j < nodeList.getLength(); j++) {
                    Node childnode1 = nodeList.item(j);
                    if (childnode1 instanceof Element) {
                        if (childnode1.getFirstChild() != null) {
                            rm.put(childnode1.getNodeName(), childnode1.getFirstChild().getNodeValue());
                        }
                        NodeList nodeList1 = childnode1.getChildNodes();    // 三级标签

                        if (nodeList1 != null) {
                            for (int k = 0; k < nodeList1.getLength(); k++) {
                                Node childnode2 = nodeList1.item(k);
                                if (childnode2 instanceof Element) {
                                    if (childnode2.getFirstChild() != null) {
                                        rm.put(childnode2.getNodeName(), childnode2.getFirstChild().getNodeValue());
                                    }
                                    NodeList nodeList2 = childnode2.getChildNodes();

                                    // 当有四级或以上标签时,在此重复写解析代码
                                }
                            }

                        }
                    }
                }

            }
        }
    }
    return rm;
}




// XML格式字符串获取指定标签值
    public static List<Map<String, String>> getXMLListValue(String result) {

        StringReader sr = new StringReader(result);    // result数据源:XML格式字符串
        InputSource is = new InputSource(sr);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        org.w3c.dom.Document document = null;
        try {
            document = builder.parse(is);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element rootElement = document.getDocumentElement();
        NodeList nodes = rootElement.getChildNodes();    // 一级标签
        List<Map<String,String>> datas = new ArrayList<>();
        for (int i = 0; i < nodes.getLength(); i++) {

            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {

                if (node.getFirstChild() != null) {
//                    rm.put(node.getNodeName(), node.getFirstChild().getNodeValue());
                }
                NodeList nodeList = node.getChildNodes();    // 二级标签
                if (nodeList != null) {
                    for (int j = 0; j < nodeList.getLength(); j++) {
                        Node childnode1 = nodeList.item(j);
                        if (childnode1 instanceof Element) {
                            if (childnode1.getFirstChild() != null) {
//                                rm.put(childnode1.getNodeName(), childnode1.getFirstChild().getNodeValue());
                            }
                            NodeList nodeList1 = childnode1.getChildNodes();    // 三级标签

                            Map<String, String> rm = new HashMap<String, String>();
                            if (nodeList1 != null) {
                                for (int k = 0; k < nodeList1.getLength(); k++) {
                                    Node childnode2 = nodeList1.item(k);
                                    if (childnode2 instanceof Element) {
                                        if (childnode2.getFirstChild() != null) {
                                            rm.put(childnode2.getNodeName(), childnode2.getFirstChild().getNodeValue());
                                        }
                                        NodeList nodeList2 = childnode2.getChildNodes();

                                        // 当有四级或以上标签时,在此重复写解析代码
                                    }
                                }
                            }
                            if (rm.size()>0) {
                                datas.add(rm);
                            }
                        }
                    }

                }

            }
        }
        return datas;
    }

      
    //XML格式字符串转为json
    public static JSONObject xmlStrToJson(String xmlString) {
        XMLSerializer xmlSerializer = new XMLSerializer();
        String resutStr = xmlSerializer.read(xmlString).toString();
        JSONObject jsonObject = JSONObject.fromObject(resutStr);
        return jsonObject;
    }
    //XML格式字符串转为map
    public static Map xmlStrToMap(String xmlString) {
        XMLSerializer xmlSerializer = new XMLSerializer();
        String resutStr = xmlSerializer.read(xmlString).toString();
        Map map = (Map) JSON.parse(resutStr);
        return map;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值