首页 > 代码库 > java对xml文件的读取

java对xml文件的读取

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <names type="1">
        <name>侵身案件</name>
        <child>
            <name>故意杀人案</name>
            <type>0</type>
        </child>
        <child>
            <name>绑架案</name>
            <type>1</type>
        </child>
        <child>
            <name>其他侵犯人身权利案</name>
            <type>2</type>
        </child>
        <child>
            <name>过失致人死亡案</name>
            <type>3</type>
        </child>
        <child>
            <name>故意伤害案</name>
            <type>4</type>
        </child>
        <child>
            <name>强奸案</name>
            <type>5</type>
        </child>
        <child>
            <name>强制猥亵、侮辱妇女案</name>
            <type>6</type>
        </child>
        <child>
            <name>非法拘禁案</name>
            <type>7</type>
        </child>
        <child>
            <name>拐卖骗妇女儿童案</name>
            <type>8</type>
        </child>
        <child>
            <name>合计</name>
            <type>9</type>
        </child>
    </names>
    <names type="2">
        <name>侵财案件</name>
        <child>
            <name>敲诈勒索案</name>
            <type>10</type>
        </child>
        <child>
            <name>其他抢劫案</name>
            <type>11</type>
        </child>
        <child>
            <name>入户抢劫案</name>
            <type>12</type>
        </child>
        <child>
            <name>故意毁坏财物案</name>
            <type>13</type>
        </child>
        <child>
            <name>盗窃汽车案</name>
            <type>14</type>
        </child>
        <child>
            <name>扒窃案</name>
            <type>15</type>
        </child>
        <child>
            <name>盗窃自行车案</name>
            <type>16</type>
        </child>
        <child>
            <name>入室盗窃案</name>
            <type>17</type>
        </child>
        <child>
            <name>盗窃电动自行车案</name>
            <type>18</type>
        </child>
        <child>
            <name>其他盗窃案</name>
            <type>19</type>
        </child>
        <child>
            <name>抢夺案</name>
            <type>20</type>
        </child>
        <child>
            <name>诈骗案</name>
            <type>21</type>
        </child>
        <child>
            <name>盗窃摩托车案</name>
            <type>22</type>
        </child>
        <child>
            <name>拦路抢劫案</name>
            <type>23</type>
        </child>
        <child>
            <name>盗窃保险柜案</name>
            <type>24</type>
        </child>
        <child>
            <name>其他侵犯财产案    </name>
            <type>25</type>
        </child>
        <child>
            <name>合计</name>
            <type>26</type>
        </child>
    </names>
    <names type="3">
        <name>妨害社会管理</name>
        <child>
            <name>阻碍执行职务案</name>
            <type>27</type>
        </child>
        <child>
            <name>毒品类案件</name>
            <type>28</type>
        </child>
        <child>
            <name>其他妨害社会管理案</name>
            <type>29</type>
        </child>
        <child>
            <name>寻衅滋事案</name>
            <type>30</type>
        </child>
        <child>
            <name>介绍容留卖淫案</name>
            <type>31</type>
        </child>
        <child>
            <name>合计</name>
            <type>32</type>
        </child>
    </names>
    <names type="4">
        <name>危害公共安全案件</name>
        <child>
            <name>防火案</name>
            <type>33</type>
        </child>
        <child>
            <name>交通肇事案</name>
            <type>34</type>
        </child>
        <child>
            <name>破坏电力电信设施案</name>
            <type>35</type>
        </child>
        <child>
            <name>其他危害公共安全案</name>
            <type>36</type>
        </child>
        <child>
            <name>合计</name>
            <type>37</type>
        </child>
    </names>
    <names type="5">
        <name>经济类案件</name>
        <type>38</type>
    </names>
    <names type="6">
        <name>总计</name>
        <type>39</type>
    </names>
</body>
private List<Ictname> Readxmlist(String filename) {
        List<Ictname> list = new ArrayList<>();
        File file = new File(filename);
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            Element root = document.getRootElement();
            List<Element> childElements = root.elements();
             for (Element child : childElements) {
                 Ictname c = new Ictname();
                 int t=Integer.parseInt(child.attributeValue("type"));
                 if(t==5){t=38;}if(t==6){t=39;}
                 c.setType(t);
                 c.setName(child.elementText("name"));
                 List<Element> nodelist = child.elements("child");
                 c.setLen(nodelist.size()+1);
                 if(nodelist.size()>0){
                     c.setChild(new ArrayList<Ictname>());
                     for (Element enode : nodelist) {
                         Ictname cc = new Ictname();
                         cc.setType(Integer.parseInt(enode.elementText("type")));
                         cc.setName(enode.elementText("name"));
                         c.getChild().add(cc);
                     } 
                 }
                 list.add(c);
             }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return list;
    }

 

java对xml文件的读取