class="java">
package com.qlsx.test;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Get
XML {
Document document;
Element root;
// String path = "f:/text.xml";
@SuppressWarnings("unchecked")
public List<PowerBean> showAllStus(String path) {
List<PowerBean> stus = new ArrayList<PowerBean>();
try {
Document document = DocumentHelper.parseText(path);//直接将xml字符串转换成document对象
// document = new SAXReader().read(path);//这是读取xml文件
//获得很节点
root = document.getRootElement();
//获得根节点的子节点的
结合
List<Element> list = root.elements();
for (Element e : list) {//获得根节点下面所有节点名和值
System.out.println(e.getName() + "======" + e.getText());
/*获取属性
System.out.println(e.attributeValue("name")+" * "+e.attributeValue("id"));
*/
// if("".equals(e.getText())){
// System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
// }
List<Element> cc = e.elements();
for(Element e2 : cc){//获得二级根节点下面所有节点名和值
List<Element> bb = e2.elements();
for (Element e3 : bb) {
System.out.println(e3.getName() + " : " + e3.getText());
}
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return stus;
}
public static void main(String[] args) {
}
}