XML读写 方法 用dom4j
程序员文章站
2022-05-29 11:42:30
...
package com.myh.kb13.t806;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
/**
* @Description:
* @author:myh
* @date: 2021/8/6 12:19
*/
public class ReadXml {
public static void main(String[] args) throws Exception {
//读取器
SAXReader reader=new SAXReader();
//读所有的文档Ducument文档对象
Document document = reader.read(new FileInputStream("d:/t.xml"));
Element root = document.getRootElement();
// System.out.println(root);
List<Element> stus=root.elements();
for (Element stu : stus){
System.out.println(stu.attributeValue("no"));
//获取Student下面的name标签和age标签 中间的值
Element name=stu.element("name");
Element age=stu.element("age");
System.out.print(name.getTextTrim()+":");//拿标签中间的值
System.out.println(age.getTextTrim());
}
//新版本自己close释放了
}
}