zoukankan      html  css  js  c++  java
  • java-读取xml

    java:

    @Override
        public Map<String, Object> getCommunityCountByRegion(String regionCode) {
            Map<String,Object> map = new HashMap<>();
            try {
                URL url = getClass().getClassLoader().getResource("community-count.xml");
                File file = new File(url.toURI());
                logger.info("community-count.xml,URL:" + file.getPath());
                SAXReader reader = new SAXReader();
                Document doc = reader.read(file);
                Element root = doc.getRootElement();
                Iterator<?> listItr = root.elementIterator("region");
                while (listItr.hasNext()){
                    Element regionNode = (Element) listItr.next();
                    if (regionCode.equals(regionNode.attribute("code").getValue())){
                        map.put("communityTotal",regionNode.element("community-total").getTextTrim());
                        map.put("communityComplete",regionNode.element("community-complete").getTextTrim());
                        map.put("villageTotal",regionNode.element("village-total").getTextTrim());
                        map.put("villageComplete",regionNode.element("village-complete").getTextTrim());
                    }
                }
            } catch (Exception e) {
                logger.error("getCommunityCountByRegion error: ",e);
            }
            return map;
        }

    xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <list>
        <region name = "XX市" code = "4101">
            <community-total>132</community-total>
            <village-total>68</village-total>
            <community-complete>10</community-complete>
            <village-complete>5</village-complete>
        </region>
        <region name = "XX县" code = "410602">
            <community-total>60</community-total>
            <village-total>8</village-total>
            <community-complete>2</community-complete>
            <village-complete>10</village-complete>
        </region>
    </list>
  • 相关阅读:
    MATLAB 之 App designer 小白学习(四)
    MATLAB 之 APP DESIGNER 学习(三)
    MATLAB 之 App designer 小白学习(二)
    MATLAB 之 App designer 小白学习(一)
    激光烧蚀 @有机聚合物
    激光驱动巨量转移工艺
    基于神经网络的函数逼近
    单片机复位电路仿真分析
    基于Python的3R机器人运动仿真
    基于MATLAB的单级倒立摆仿真
  • 原文地址:https://www.cnblogs.com/lijianda/p/13895068.html
Copyright © 2011-2022 走看看