创建一个XML文件,向其中添加Student类型的元素,再对其中的元素进行操作。
Student类
package com.xml;
public class Student {
String name;
String sex;
String school;
public Student(String name,String sex,String school){
this.name=name;
this.sex=sex;
this.school=school;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
操作类
package com.xml;
import java.io.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.util.*;
/**XML的解析
* 对XML文件进行增删改查操作
* @author Administrator
*
*/
public class Test {
public static void creatXml() throws FileNotFoundException, IOException{
Document d=new Document();
Element e=new Element("Student");
e.addContent(new Element("Student")
.addContent(new Element("name").setText("张三"))
.addContent(new Element("sex").setText("男"))
.addContent(new Element("school").setText("桂工")));
d.addContent(e);
XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
o.output(d, new FileOutputStream(new File("student.xml")));
}
public static void addXML(Student s)throws Exception{
SAXBuilder b=new SAXBuilder();
Document doc=b.build(new File("student.xml"));
Element e=doc.getRootElement();
e.addContent(new Element("student")
.addContent(new Element("name").setText(s.getName()))
.addContent(new Element("sex").setText(s.getSex()))
.addContent(new Element("school").setText(s.getSchool()))
);
System.out.println("添加成功");
XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
o.output(doc, new FileOutputStream(new File("student.xml")));
}
public static void updateXml(String oldName,String newName)throws Exception{
SAXBuilder b=new SAXBuilder();
Document doc=b.build(new File("student.xml"));
Element e=doc.getRootElement();
List<Element> list=e.getChildren();
for (int i = 0; i < list.size(); i++) {
Element ee=list.get(i);
if(ee.getChildText("name").equals(oldName)){
ee.getChild("name").setText(newName);
System.out.println("修改成功");
}
}
XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
o.output(doc, new FileOutputStream(new File("student.xml")));
}
public static void delXml(String name) throws JDOMException, IOException{
SAXBuilder b=new SAXBuilder();
Document doc=b.build(new File("student.xml"));
Element e=doc.getRootElement();
List<Element> list=e.getChildren();
for (int i = 0; i < list.size(); i++) {
Element ee=list.get(i);
if(ee.getChildText("name").equals(name)){
e.removeContent(ee);
System.out.println("删除成功");
}
}
XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
o.output(doc, new FileOutputStream(new File("student.xml")));
}
public static void findByName(String name) throws JDOMException, IOException{
SAXBuilder b=new SAXBuilder();
Document doc=b.build(new File("student.xml"));
Element e=doc.getRootElement();
List<Element> list=e.getChildren();
for (int i = 0; i < list.size(); i++) {
Element ee=list.get(i);
if(ee.getChildText("name").equals(name)){
System.out.println(ee.getChildText("name"));
System.out.println(ee.getChildText("sex"));
System.out.println(ee.getChildText("school"));
}
}
XMLOutputter o=new XMLOutputter(Format.getPrettyFormat().setEncoding("utf-8").setIndent(" "));
o.output(doc, new FileOutputStream(new File("student.xml")));
}
public static void main(String[] args) throws Exception{
findByName("小花花");
}
}