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>
  • 相关阅读:
    使用winmm.dll 获取麦克风声音数据
    什么是拆箱和装箱?
    C#窗体程序【用户控件-窗体】委托事件
    如何在网页标题栏加入logo图标?
    C#汉字转拼音帮助类
    JQuery中$.ajax()方法参数详解
    UEditor独立图片、文件上传模块
    SqlServer2008安装时提示重启计算机失败 解决办法
    如果说人生是自我编写的程序
    LINQ的Any() 方法
  • 原文地址:https://www.cnblogs.com/lijianda/p/13895068.html
Copyright © 2011-2022 走看看