package forRQ;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Main {
public Document read(String fileName) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
return document;
}

public static void main(String args[]) throws Exception {

String str = "rqxml/table.xml";
Main main = new Main();
Document doc = main.read(str);
Element root = doc.getRootElement();
List<?> tables = root.elements("table");// 根据根节点,将根节点下
for (Object table : tables) {//
Element etable = (Element) table;
System.out.println(etable.getName()+"-----");// 获取节点下的数据。
List<?> fields = etable.elements("field");// 获取节点下所有的内容,存入容器中
List<?> attrs = etable.attributes();
for (Object attr : attrs) {
Attribute cattr = (Attribute) attr;
System.out.println(cattr.getName() + ": "+ cattr.getText());// 获取节点下的数据。
}
for (Object field : fields) {
Element efield = (Element) field;
List<?> fattrs = efield.attributes();
System.out.println(efield.getName() + ": >>>>>");// 获取节点下的数据。
for (Object attr : fattrs) {
Attribute cattr = (Attribute) attr;
System.out.println(cattr.getName() + ": "+ cattr.getText());// 获取节点下的数据。
}
}
}
}
}

05-28 20:03