package com.tarena.day22;
import java.io.FileOutputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class TestStudent {
public static void main(String[] args) throws Exception{
String filePath = "/home/soft01/xml_day2/student.xml";
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(filePath);
Element el = doc.getRootElement();//得到根节点
List<Element> list = el.getChildren("student");//根节点集合,指的是student的集合;下面还可以有breakLine的集合
for(Element student : list){
// String name = student.getChildTextTrim("name");
// System.out.println(name);
Element ele = student.getChild("name");//取单个的用child,集合用children
System.out.println(ele.getText());
ele.setText("吕布");
String sex = student.getChildTextTrim("sex");
System.out.println(sex);
// Element ele2 = student.getChild("sex");
// ele2.setText("女");
List<Element> list2 = student.getChildren("lesson");
for(Element lesson : list2){
String lessonName = lesson.getChildTextTrim("lessonName");
System.out.println(lessonName);
String lessonScore = lesson.getChildTextTrim("lessonScore");
System.out.println(lessonScore);
}
// Element breakLine = student.getChild("breakLine");
// System.out.println(breakLine.getText());
}
List<Element> bre = el.getChildren("breakLine2");
for(Element brea : bre){
// Element line = brea.getChild("lin");
System.out.println(brea.getText());
}
XMLOutputter out = new XMLOutputter();
out.output(doc, new FileOutputStream(filePath));
}
}