使用Jdom对xml文件进行基本操作

使用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文档需要的可到官网下载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值