读取XML文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.app;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
public class Rule {
 
    /**
     * 分类规则内容,键值为类别(大类#中类#小类),Value值为关键字
     */
    private LinkedHashMap<String, ArrayList<KeyWords>> rule = new LinkedHashMap<String, ArrayList<KeyWords>>();
 
    /**
     * 加载分类规则文件
     *
     * @param path
     */
    public Rule(String path) {
        if (this.rule.isEmpty()) {
            try {
                loadXml(path);
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 使用dom4j 中saxreader 获取Document容器,利用此容器的elementIterator读取xml文件
     */
    public void loadXml(String rulePath) throws DocumentException {
        // 获取读取xml的对象
        SAXReader sr = new SAXReader();
        // 得到xml所在位置,然后开始读取,并将数据放入doc中
        Document doc = sr.read(rulePath);
        // 向外取数据,获取xml的根节点
        Element root = doc.getRootElement();
        ArrayList<KeyWords> keyWords = new ArrayList<KeyWords>();
        iteElement(root, "", keyWords);
    }
 
    public void iteElement(Element element, String className,
            ArrayList<KeyWords> keyWords) {
        // 遍历该子节点
        Iterator it = element.elementIterator();
        while (it.hasNext()) {
            ArrayList<KeyWords> keyWords_clone = (ArrayList<KeyWords>) keyWords
                    .clone();
            // 获取节点
            Element firstClass = (Element) it.next();
            // 到达叶子节点
            if (firstClass.elements().size() == 0) {
                String word = firstClass.getText();
                String weight = firstClass.attributeValue("weight");
                KeyWords words = new KeyWords(new HashSet<String>(
                        Arrays.asList(word.split("\\s+"))),
                        Double.valueOf(weight));
                keyWords_clone.add(words);
                rule.put(className, keyWords_clone);
                return;
            } else {
                String dalei = firstClass.attributeValue("name");
                String feature = firstClass.attributeValue("feature");
                String weight = firstClass.attributeValue("weight");
                KeyWords firWords = new KeyWords(new HashSet<String>(
                        Arrays.asList(feature.split("\\s+"))),
                        Double.valueOf(weight));
                keyWords_clone.add(firWords);
                // 递归调用
                if (className.length() < 1) {
                    iteElement(firstClass, className + dalei, keyWords_clone);
                } else {
                    iteElement(firstClass, className + "#" + dalei,
                            keyWords_clone);
                }
 
            }
 
        }
 
    }
 
    /**
     * 每一类别的规则关键词
     */
    class KeyWords {
        /**
         * 关键词列表
         */
        HashSet<String> value;
        /**
         * 权重
         */
        double weight;
 
        public KeyWords(HashSet<String> value, double weight) {
            this.value = value;
            this.weight = weight;
        }
 
        /**
         * @return the value
         */
        public HashSet<String> getValue() {
            return value;
        }
 
        /**
         * @param value
         *            the value to set
         */
        public void setValue(HashSet<String> value) {
            this.value = value;
        }
 
        /**
         * @return the weight
         */
        public double getWeight() {
            return weight;
        }
 
        /**
         * @param weight
         *            the weight to set
         */
        public void setWeight(double weight) {
            this.weight = weight;
        }
 
    }
 
    /**
     * @return the rule
     */
    public LinkedHashMap<String, ArrayList<KeyWords>> getRule() {
        return rule;
    }
 
    public static void main(String[] args) {
        Rule r = new Rule("rule2.xml");
        LinkedHashMap<String, ArrayList<KeyWords>> rule = r.getRule();
        for (String className : rule.keySet()) {
            System.out.println(className + "---------------");
            ArrayList<KeyWords> keyWords = rule.get(className);
            for (KeyWords words : keyWords) {
                HashSet<String> value = words.getValue();
                System.out.println(value.toString());
            }
        }
    }
}

  

posted on   ywf—java  阅读(182)  评论(0编辑  收藏  举报

导航

点击右上角即可分享
微信分享提示