DOM4J解析XML文件
public class DOM4JTest1 {
public static void main(String[] args) throws DocumentException {
//1.创建解析的类
SAXReader reader = new SAXReader();
//2.开始解析 形成文档树
Document document = reader.read("D:/mycode/XML/teacher/students2.xml");
//3.获取根节点
Element rootElement = document.getRootElement();
//4.获取字节点
List<Element> students = rootElement.elements("student");
for (Element student : students) {
//5.获取属性
//获取指定的属性
Attribute attribute = student.attribute("id");
System.out.println(attribute.getName()+"<===>"+attribute.getValue());
/*
获取所有的属性
List<Attribute> attributes = student.attributes();
for (Attribute attribute : attributes) {
System.out.println(attribute.getName()+"<===>"+attribute.getValue());
}*/
//6.获取孙子元素
List<Element> childStudents = student.elements();
//7.获取孙子元素的内容
childStudents.forEach((x)->{
System.out.println( x.getName()+"<======>"+x.getText());
});
/* for (Element x : childStudents) {
System.out.println( x.getName()+"<======>"+x.getText());
}*/
System.out.println();
}