Use Jdom and XPath to create, find, add, delete and save XML files

Source: Internet
Author: User
Tags rar xpath
Today I'm going to write a multithreaded breakpoint download program in the form of a generic jar file, but to consider the user has downloaded no download to complete the preservation of the file saved, think of several ways, one is a text file, consider reading and writing up more trouble, want to read in a row of all the analysis, give up; the second is to use the database , but considering that not all users like it, and the database also occupies a larger space, give up; the third way is to use the XML, think of this way of the transplant is strong, many platforms are supported, and read and write up is also more convenient, so decided to use this way, the following is today plus a little experience , including the creation of XML files, find, add, delete, save and other operations, first put here, and later useful, then to take, note, to run the following program, please download Jdom first, and the environment variable:

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;

/**

* Create an XML file dynamically in this class to save the downloaded file name and path

* So that the breakpoint renewal function of these downloads can be used

* The XML file format created is as follows:

* <?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; Full path to XML document

/**

*

* @param path for downloading temporary files

*/

Public Filedowninformationwithxml (String Path) {

Builder = new saxbuilder ();

this. Fileandpath = Path + xmlfilename;

if (checkxmlfileexist () = false) {

if (Createxmlfile () = false) {

SYSTEM.OUT.PRINTLN ("XML file creation error, make sure you have permission");

return;

}

}

Try {

Document = Builder.build (Fileandpath);

Catch (Exception e) {

E.printstacktrace ();

}

root = Document.getrootelement ();

System.out.println ("Root:" +root.getname ());

}

/**

* Returns the local address obtained based on the network address, which is used at the breakpoint continuation

* If the null value is returned, then it means that there is no download list and should be downloaded again

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.