![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
|||||
| 利用JDOM及XPath对XML文件进行创建、查找、增加、删除、保存等操作 | |||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-2 ![]() |
|||||
|
正在装载数据…… 今天准备把原来写的多线程断点下载程序写成通用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; } /** * 检查当前XML是否存在 * @return */ public boolean checkXMLFileExist() { File file = new File(fileAndPath); if (file.exists()) return true; else return false; } /** * 在XML文件不存在的情况,那么就要建立一个XML文件 */ public //注:这里不能够用newFile的方法来创建,必须指定一个根,否则要出错 //最好采用下面的方式创建 boolean createXMLFile() { try { Element e = new Element("downFile"); Document doc = new Document(e); saveChange(doc); //保存创建的文件 return true; } catch (Exception e) { return false; } } } 本文来源:http://blog.csdn.net/fenglibing/archive/2007/07/08/1683046.aspx
|
|||||
| 文章录入:admin 责任编辑:admin | |||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | |||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|