前言:菜鸟程序,只为更好记忆。
编写代码之前要去下载jdom.jar包
1、管理类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
* 使用Jdom来生成XML文档结构,并进行增删改查
* @author Administrator
*
*/
public class XmlManager {
/**加载数据,从文件中读取数据(文件中数据格式为:(id号/姓名/年龄/市区/省区))
* @author Administrator
*@param File
*/
List<Person> list = new ArrayList<Person>();//用来暂存文件读取到的信息
public void MyIO(File file) {
try {
FileReader read = new FileReader(file);
BufferedReader in = new BufferedReader(read);
String s = "";
while ((s = in.readLine()) != null) {
String[] arr= s.split("/");
list.add(new Person(arr[0],Integer.parseInt(arr[1]),arr[2],arr[3]));
}
in.close();
read.close();
} catch (IOException e) {
e.getMessage();
}
System.out.println("数据加载成功!");
System.out.println(list.size());
}
/**生成xml文档
* @author Administrator
*@param
* @throws IOException
* @throws FileNotFoundException
*/
public void getMyXML() throws FileNotFoundException, IOException {
//创建文档
Document doc=new Document();
//创建根节点
Element rootElement=new Element("Persons");
//在根节点下创建子节点
for(int i=0;i<list.size();i++) {
rootElement.addContent(new Element("person")
.addContent(new Element("name").setText(list.get(i).getName()))
.addContent(new Element("age").setText(String.valueOf(list.get(i).getAge())))
.addContent(new Element("address").setText(list.get(i).getAddress()))
.addContent(new Element("city").setText(list.get(i).getCity()))
);
}
//把根节点加入到文档
doc.addContent(rootElement);
//生成xml文档(IO流)
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));
output.output(doc, new FileOutputStream(new File("person.xml")));
System.out.println("操作成功!!");
}
/**对xml文档进行----增操作
* @author Administrator
*@param File
* @throws IOException
* @throws JDOMException
*/
public void addXML(Person p) throws JDOMException, IOException {
//创建工厂
SAXBuilder builder=new SAXBuilder();
//获取xml文档
Document doc=builder.build(new File("person.xml"));
//获取根节点
Element rootElement= doc.getRootElement();
//往根节点中加子节点
rootElement.addContent(new Element("person")
.addContent(new Element("name").setText(p.getName()))
.addContent(new Element("age").setText(String.valueOf(p.getAge())))
.addContent(new Element("address").setText(p.getAddress()))
.addContent(new Element("city").setText(p.getCity()))
);
//重新保存
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));
output.output(doc, new FileOutputStream(new File("person.xml")));
System.out.println("添加成功!");
}
/**对xml文档进行----修改操作(根据名字)
* @author Administrator
*@param oldName,newName
* @throws IOException
* @throws JDOMException
*/
public void updateXML(String oldName,String newName) throws JDOMException, IOException {
//创建工厂
SAXBuilder builder=new SAXBuilder();
//获取xml文档
Document doc=builder.build(new File("person.xml"));
//获取根节点
Element rootElement= doc.getRootElement();
//获取根节点下的所有子节点
List<Element> eList=rootElement.getChildren();
//遍历子节点
for(int i=0;i<eList.size();i++) {
Element e=eList.get(i);//获取根节点下的一个子节点
if(e.getChildText("name").equals(oldName)) {//e.getChildText("name")===获取子节点下的name子元素的值
e.getChild("name").setText(newName);//获取子节点下的子元素,重新复制为newName
}
}
//重新保存
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));
output.output(doc, new FileOutputStream(new File("person.xml")));
System.out.println("修改成功!");
}
/**对xml文档进行----删除操作(根据名字)
* @author Administrator
*@param name
* @throws IOException
* @throws JDOMException
*/
public void delXML(String name) throws JDOMException, IOException {
//创建工厂
SAXBuilder builder=new SAXBuilder();
//获取xml文档
Document doc=builder.build(new File("person.xml"));
//获取根节点
Element rootElement= doc.getRootElement();
//获取根节点下的所有子节点
List<Element> eList=rootElement.getChildren();
//遍历子节点
for(int i=0;i<eList.size();i++) {
Element e=eList.get(i);//获取根节点下的一个子节点
if(e.getChildText("name").equals(name)) {//e.getChildText("name")===获取子节点下的name子元素的值
rootElement.removeContent(e) ; //从根节点下删除指定子节点
}
}
//重新保存
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));
output.output(doc, new FileOutputStream(new File("person.xml")));
System.out.println("删除成功!");
}
/**对xml文档进行----删除操作(根据名字)
* @author Administrator
*@param name
* @throws IOException
* @throws JDOMException
*/
public void findByName(String name) throws JDOMException, IOException {
//创建工厂
SAXBuilder builder=new SAXBuilder();
//获取xml文档
Document doc=builder.build(new File("person.xml"));
//获取根节点
Element rootElement= doc.getRootElement();
//获取根节点下的所有子节点
List<Element> eList=rootElement.getChildren();
//遍历子节点
for(int i=0;i<eList.size();i++) {
Element e=eList.get(i);//获取根节点下的一个子节点
System.out.println(e.getChildText("name"));
System.out.println(e.getChildText("name").equalsIgnoreCase(name));
if(e.getChildText("name").equals(name)) {//e.getChildText("name")===获取子节点下的name子元素的值
System.out.println(e.getChildText("name")); //输出子节点下name子元素下的信息
}
}
//重新保存
XMLOutputter output=new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8").setIndent(" "));
output.output(doc, new FileOutputStream(new File("person.xml")));
System.out.println("查询成功!");
}
}
二、实体类
/**实体类--人
* @author Administrator
*
*/
public class Person {
private String name;
private int age;
private String address;
private String city;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String address, String city) {
super();
this.name = name;
this.age = age;
this.address = address;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
3、测试类
/**测试类
* @author Administrator
*
*/
public class XMLTest {
public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {
XmlManager manager=new XmlManager();
File file=new File("d:/Mydoc.txt");
//manager.MyIO(file); //加载数据
//manager.getMyXML(); //生成xml文档
//manager.addXML(new Person("小白",22,"深圳","广东"));//新增元素
//manager.updateXML("小白","小小白");
//manager.delXML("小张");
manager.findByName("小熊");
}
}