解析读取XML

public static void main(String[] args) {
//1获取SaxReader对象
SAXReader reader=new SAXReader();
try {
//2使用Saxrreader对象加载xml文件,返回Document对象
Document document=reader.read(new File("student.xml"));

//3使用document对象或根元素
Element rootele=document.getRootElement();

//4获取子元素的集合(获取的是student子元素的集合)
List<Element> lsstu_ele=rootele.elements("student");

System.out.println("student元素的个数:"+lsstu_ele.size());
//遍历student元素
for (Element stuele : lsstu_ele) {
/***********************************
//获取student元素下的子元素
Element sid_ele=stuele.element("sid");
Element sname_ele=stuele.element("sname");
Element sex_ele=stuele.element("sex");
Element address_ele=stuele.element("address");
Element age_ele=stuele.element("age");

//获取每个子元素的文本内容
System.out.println("编号:"+sid_ele.getText());
System.out.println("姓名:"+sname_ele.getText());
System.out.println("性别:"+sex_ele.getText());
System.out.println("地址:"+address_ele.getText());
System.out.println("年龄:"+age_ele.getText());
System.out.println("========================");
**************************************/
//获取每个子元素的文本内容
System.out.println("编号:"+stuele.elementText("sid"));
System.out.println("姓名:"+stuele.elementText("sname"));
System.out.println("性别:"+stuele.elementText("sex"));
System.out.println("地址:"+stuele.elementText("address"));
System.out.println("年龄:"+stuele.elementText("age"));
System.out.println("========================");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

向xml文件中添加节点并写入文件保存;

public static void main(String[] args) {
//1创建SaxReader对象
SAXReader reader=new SAXReader();

try {
//2读取xml文档,并返回document对象
Document document=reader.read(new File("student.xml"));

//3获取根元素
Element rootele=document.getRootElement();

//4在根元素下添加student子元素
Element st_ele=rootele.addElement("student");

//在新增的student元素下添加其他子元素
Element sid_ele=st_ele.addElement("sid");
Element sname_ele=st_ele.addElement("sname");
Element sex_ele=st_ele.addElement("sex");
Element address_ele=st_ele.addElement("address");
Element age_ele=st_ele.addElement("age");

//在每个子元素下添加内容
Scanner sc=new Scanner(System.in);

System.out.println("输入编号:");
sid_ele.setText(sc.next());

System.out.println("输入姓名:");
sname_ele.setText(sc.next());

System.out.println("输入性别:");
sex_ele.setText(sc.next());

System.out.println("输入地址:");
address_ele.setText(sc.next());

System.out.println("输入年龄:");
age_ele.setText(sc.next());

//创建字节输出流对象
FileOutputStream fout=new FileOutputStream("student.xml");

//创建缓冲区字符输出流对象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8"));

//写文档到xml文件
document.write(bw);
bw.flush();
bw.close();
System.out.println("增加成功!");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

修改xml 文件中的节点并写入文件保存;

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//1创建SaxReader对象
SAXReader reader=new SAXReader();

try {
//2读取xml文档,并返回document对象
Document document=reader.read(new File("student.xml"));

//3获取根元素
Element rootele=document.getRootElement();

//4获取student子元素的集合
List<Element> lsst_ele=rootele.elements("student");

System.out.println("请输入要修改的学生编号:");
String sid=sc.next();

for (Element stu_ele : lsst_ele) {
if(stu_ele.elementText("sid").equals(sid)){
System.out.println("输入姓名:");
stu_ele.element("sname").setText(sc.next());

System.out.println("输入性别:");
stu_ele.element("sex").setText(sc.next());

System.out.println("输入地址:");
stu_ele.element("address").setText(sc.next());

System.out.println("输入年龄:");
stu_ele.element("age").setText(sc.next());
break;
}
}

//创建字节输出流对象
FileOutputStream fout=new FileOutputStream("student.xml");

//创建缓冲区字符输出流对象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8"));

//写文档到xml文件
document.write(bw);
bw.flush();
bw.close();
System.out.println("修改成功!");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

删除xml文件中的节点

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//1创建SaxReader对象
SAXReader reader=new SAXReader();

try {
//2读取xml文档,并返回document对象
Document document=reader.read(new File("student.xml"));

//3获取根元素
Element rootele=document.getRootElement();

//获取要移除的学生编号
System.out.println("请输入要删除的学生编号:");
String sid=sc.next();

//获取student元素的集合
List<Element> lsstu_ele=rootele.elements("student");
for (Element stu_ele : lsstu_ele) {
//在元素中查找学生编号内容是否与输入的一致
if(stu_ele.elementText("sid").equals(sid)){
rootele.remove(stu_ele);//删除整个student元素
System.out.println("元素删除成功!");
break;
}
}
//创建字节输出流对象
FileOutputStream fout=new FileOutputStream("student.xml");

//创建缓冲区字符输出流对象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8"));

//写文档到xml文件
document.write(bw);
bw.flush();
bw.close();
System.out.println("移除成功!");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

 xml读写的工具类方法示例

/**
* 将Document对象写入到xml文件
* @throws IOException
* */
public static void XmlToFile(Document doc) throws IOException{

//创建字节输出流对象
FileOutputStream fout=new FileOutputStream(new File("student.xml"));
//创建字节流缓冲区,加快写出速度
BufferedOutputStream bout=new BufferedOutputStream(fout);

//创建字符输出流对象
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(bout, "utf-8"));

doc.write(bw);

bw.flush();
bw.close();
System.out.println("写xml文件成功");
}
/**
* 从xml文件中获取学生的对象集合
* */
public static List<Student> fileToList(){
SAXReader reader=new SAXReader();
List<Student> lsst=new ArrayList<Student>();

try {
Document doc=reader.read(new File("student.xml"));

//获取根元素
Element root_ele=doc.getRootElement();


//从xml文件获取student子元素集合
List<Element> lsst_ele=root_ele.elements("student");
if(lsst_ele!=null&&!lsst_ele.isEmpty()){//有student子元素存在
for (Element stu_ele : lsst_ele) {
Student st=new Student();
st.setSid(stu_ele.elementText("sid"));
st.setSname(stu_ele.elementText("sname"));
st.setSex(stu_ele.elementText("sex"));
st.setAddress(stu_ele.elementText("address"));
st.setAge(Integer.parseInt(stu_ele.elementText("age")));

lsst.add(st);//将学生对象加入到集合中
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lsst;
}