使用Jdom对xml文件进行基本操作演示
注:创建后的XML文件形式如下,假设ID为主键:
<?xml version="1.0" encoding="UTF-8"?> <class> <student id="2009"> <name>coolszy</name> <gender>boy</gender> <birthday>1988-01-01</birthday> </student> </class>
首先创建一个Student类,包含了student的基本属性
package com.szy.xmloperation;
public class Student
{
private String id;
private String name;
private String gender;
private String birthday;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getBirthday()
{
return birthday;
}
public void setBirthday(String birthday)
{
this.birthday = birthday;
}
}
对XML进行基本的操作,一些其它的操作类似,在此就没有写出
package com.szy.xmloperation;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format.TextMode;
public class XMLOperation
{
/**
* 创建xml文件
* @param xml文件的路径+文件名
* @throws Exception
*/
public void create(String filePath) throws Exception
{
Document doc=null;
Element root=null;
root=new Element("class"); //创建跟节点
doc=new Document(root);
outPut(doc, filePath); //输出到xml文件中
}
/**
* 往xml文件中添加新节点
* @param Student对象
* @param xml文件的路径+文件名
* @throws Exception
*/
public void addNode(Student student,String filePath) throws Exception
{
Document doc=null;
Element root=null;
SAXBuilder sb=new SAXBuilder(false);
doc=sb.build(new FileInputStream(new File(filePath)));
root=doc.getRootElement(); //获取根元素
Element studentElement=new Element("student");
studentElement.setAttribute("id",student.getId());
Element namElement=new Element("name");
namElement.addContent(student.getName());
studentElement.addContent(namElement);
Element genderElement=new Element("gender");
genderElement.addContent(student.getGender());
studentElement.addContent(genderElement);
Element birthdayElement=new Element("birthday");
birthdayElement.addContent(student.getBirthday());
studentElement.addContent(birthdayElement);
root.addContent(studentElement);
outPut(doc, filePath);
}
/**
* 修改节点属性的值
* @param 属性名
* @param 原来的值
* @param 新的值
* @param xml文件的路径+文件名
* @throws Exception
*/
public void modifyProperty(String propertyName,String oldValue,String newValue,String filePath) throws Exception
{
Document doc=null;
Element root=null;
SAXBuilder sb=new SAXBuilder(false);
doc=sb.build(new FileInputStream(new File(filePath)));
root=doc.getRootElement(); //获取根元素
List<Element> list=root.getChildren("student");
for (Element element : list)
{
if (element.getAttributeValue(propertyName).equals(oldValue))
{
element.setAttribute(propertyName, newValue);
break;
}
}
outPut(doc, filePath);
}
public void deleteNode(String id,String filePath) throws Exception
{
Document doc=null;
Element root=null;
SAXBuilder sb=new SAXBuilder(false);
doc=sb.build(new FileInputStream(new File(filePath)));
root=doc.getRootElement(); //获取根元素
List<Element> list=root.getChildren("student");
for (Element element : list)
{
if (element.getAttributeValue("id").equals(id))
{
root.removeContent(element);
break;
}
}
outPut(doc, filePath);
}
/**
* 把结果保存到xml文件中
* @param doc Document对象
* @param filePath xml文件路径+文件名
* @throws Exception
*/
private void outPut(Document doc,String filePath) throws Exception
{
XMLOutputter output=new XMLOutputter();
Format format=output.getFormat();
format.setEncoding("UTF-8"); //设置编码
format.setIndent("\t");
format.setLineSeparator(System.getProperty("line.separator"));
format.setTextMode(TextMode.TRIM_FULL_WHITE); //去掉后观察效果
format.setExpandEmptyElements(true);
output.setFormat(format);
FileWriter writer=new FileWriter(filePath);
output.output(doc, writer);
writer.close();
}
}
附上源码,包含Jdom包,API文档需要的可到官网下载。