package com.hwq;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
public class ReadConfig {
public Document readXML() {
Document document = null;
SAXReader saxReader = new SAXReader();
try {
document = saxReader.read(new File("D:/hwq.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
public void disXML_3(Element element) {
Iterator iterator = element.elementIterator();
List list = null;
while (iterator.hasNext()) {
Element node = (Element) iterator.next();
list = node.elements();
if (list != null && list.size() > 0) {
System.out.print(node.getName());
int count = node.attributeCount();
for (int i = 0; i < count; i++) {
Attribute attr = node.attribute(i);
System.out.println("*****" + attr.getName() + " " + attr.getText());
}
disXML_3(node);
} else {
System.out.println(node.getName() + " " + node.getText());
}
}
}
public void getRoot(Document document) {
Element root = document.getRootElement();
disXML_3(root);
}
public static void main(String[] args) {
ReadConfig d = new ReadConfig();
d.getRoot(d.readXML());
}
}