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);
}
}