利用JDOM及XPath操作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;

        }

    }

}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值