- 我采用的是dom4j里面的Element来读取的,Maven依赖如下
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
InputStream inputStream = null;
SAXReader saxReader = new SAXReader();
try {
Path path = Paths.get(一个URL);
inputStream = Files.newInputStream(path);
Document document = saxReader.read(inputStream);
Element root = document.getRootElement();//根节点
Element ruleEle = null;
for (Iterator<Element> it = ruleEles.elementIterator("rule"); it
.hasNext();) {
ruleEle = (Element) it.next();
//获取rule节点下所有的name属性的值
String name = ruleEle.attribute("name").getValue();
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}