前言
使用dom4j
库
java读xml文件
示例代码:
SAXReader saxReader = new SAXReader();
Document doc = null;
try {
FileInputStream fin = new FileInputStream("D://doc_f/student.xml");
document = saxReader.read(fin);
Element root = document.getRootElement(); // 获取根元素
System.out.prinln(root.getName());
List<Nodes> nodes = root.selectNodes("/Index/Items"); // xpath路径
Element element = (Element) root.selectNodes("Index/Items").get(0);
List<Element> elements = element.elements();
for(Element element1 : elements) {
System.out.println(elemen1.attribute("name").getValue()); // 元素有名为name的属性,其值为.getValue()的结果
}
} catch (...) {
...
}
标签:xml,Index,elements,Java,读写,Element,root
From: https://www.cnblogs.com/rdisheng/p/18231441