public static void main(String[] args) throws DocumentException {
SAXReader saxReader = new SAXReader();
Document read = saxReader.read(new File("E://work//spring-ioc//src//main//resources//stu.xml"));
// 获取根节点
Element rootElement = read.getRootElement();
getNodes(rootElement);
}
static public void getNodes(Element rootElement) {
System.out.println("当前节点名称:" + rootElement.getName());
// 获取属性ID
List<Attribute> attributes = rootElement.attributes();
for (Attribute attribute : attributes) {
System.out.println("属性:" + attribute.getName() + "---" + attribute.getText());
}
if (!rootElement.getTextTrim().equals("")) {
System.out.println(rootElement.getName() + "--" + rootElement.getText());
}
// 使用迭代器遍历
Iterator<Element> elementIterator = rootElement.elementIterator();
while (elementIterator.hasNext()) {
Element next = elementIterator.next();
getNodes(next);
}
}