利用JDOM及XPath对XML文件进行创建、查找、增加、删除、保存等操作

本文介绍了一种使用XML文件记录已下载文件信息的方法,适用于断点续传功能。探讨了XML文件的设计与实现,包括创建、读取、更新和删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天准备把原来写的多线程断点下载程序写成通用JAR文件的形式,但是要考虑用户已经下载没有下载完成的文件保存的保存,想了几种方式,一种是文本文件,考虑读写起来比较麻烦,要一行一行的全部读进来再分析,放弃;第二种是采用数据库,但是考虑到并不是所有的用户都喜欢这种方式,并且数据库还占空间较大,放弃;第三种方式就是采用XML方式,想到这种方式的移植性强,很多平台都支持,并且读写起来也是比较方便,所以就决定采用这种方式,以下是今天加前面的一点心得,包括了对 XML文件进行创建、查找、增加、删除、保存等操作,先放在这里,等以后有用的时候,再来取,注,要运行下面程序,请先下载JDOM,并配好环境变量

import java.io.File;

import java.io.FileOutputStream;

import java.util.List;

import org.jdom.Attribute;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

import org.jdom.xpath.XPath;

/**

* 在该类中将动态的创建XML文件用以保存下载的文件名及路径

* 以便这些下载文件的断点续传功能可以使用

* 创建的XML文件格式如下:

* <?xml version="1.0" encoding="UTF-8" ?>

<downFile>

<File internetPath="http://www.123.com/xx.rar">

<localPath>c:\\tmp</localPath>

</File>

<File internetPath="http://www.123.com/xx2.rar">

<localPath>c:\\tmp0</localPath>

</File>

</downFile>

*/

public class FileDownInformationWithXML {

SAXBuilder builder;

Document document;

Element root;

String XMLFileName = "downFile.xml";

String fileAndPath; //XML文档的完整路径

/**

*

* @param Path 存放下载临时文件的路径

*/

public FileDownInformationWithXML(String Path) {

builder = new SAXBuilder();

this.fileAndPath = Path + XMLFileName;

if (checkXMLFileExist() == false) {

if (createXMLFile() == false) {

System.out.println("XML文件创建错误,请确认您是否有权限");

return;

}

}

try {

document = builder.build(fileAndPath);

} catch (Exception e) {

e.printStackTrace();

}

root = document.getRootElement();

//System.out.println("root:"+root.getName());

}

/**

* 返回根据网络地址得到的本地地址,这是在断点续传中使用

* 如果返null值,那么就说明该没有不存在下载列表中,就应该重新下载

*/

public String downFileExist(String internetAddress) {

String localPath = null;

XPath xPath; //查找id=1的是不是存在

try {

//下面是查找文件名及下载地址都相同的项

//xPath = XPath.newInstance("/downFile/File[@name='"+file+"']/internetPath[text()='"+internetAddress+"']");

xPath =

XPath.newInstance("//File[@internetPath='" + internetAddress +

"']");

List list = xPath.selectNodes(document);

if (list.size() == 1) {

Element e = (Element)list.get(0);

localPath = e.getChildText("localPath");

}

} catch (Exception e) {

e.printStackTrace();

}

return localPath;

}

/**

* 根据网络地址及本地地址,增加一个节点

*/

public void addOneDownRecord(String internetAddress, String localAddress) {

Element e = new Element("File");

root.addContent(e);

Attribute attribute = new Attribute("internetPath", internetAddress);

e.setAttribute(attribute);

Element e1 = new Element("localPath");

e1.setText(localAddress);

e.addContent(e1);

}

/**

* 根据网络地址删除一个节点

*/

public boolean removeOneDownRecord(String internetAddress) {

XPath xpath;

try {

xpath =

XPath.newInstance("//File[@internetPath='" + internetAddress +

"']");

List list = xpath.selectNodes(root);

if (list.size() == 1) {

Element e = (Element)list.get(0);

root.removeContent(e);

return true;

}

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 将更改的结果保存到磁盘

* @return

*/

public boolean saveChange() {

XMLOutputter out;

try {

out = new XMLOutputter();

out.output(root, new FileOutputStream(fileAndPath));

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

public boolean saveChange(Document doc) {

XMLOutputter out;

try {

out = new XMLOutputter();

out.output(doc, new FileOutputStream(fileAndPath));

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值