Read and write XML files using dom4j

Source: Internet
Author: User


dom4j is a Java XML API, similar to Jdom, used to read and write XML files. DOM4J is a very good JavaXML API that features excellent performance, power, and ease of use, and is also an open source software.

String filename= "D:\\version.xml";

File Inputxml=new file (fileName);

Parsing XML documents using Saxreader Version.xml

Saxreader saxreader=new Saxreader ();

Saxreader.setencoding ("UTF-8");

Document Document=saxreader.read (Inputxml);

Element params=document.getrootelement ();//root node

list<element> elementlist = params.elements ();

Loop through the nodes for processing

for (int i = 0; i < elementlist.size (); i++) {

element param = (element) elementlist.get (i);

Element key = Param.element ("key");

Remove the value of a specific node

if ("AAAAA". Equals (Key.gettexttrim ())) {

params.remove (param);

break;

}

Params.remove (param); Remove a node

}

Add a Node

Element param = params.addelement ("param");

Element Paramkey = param.addelement ("key");

Paramkey.settext ("project");

Element paramvalue = param.addelement ("value");

Output XML file

OutputFormat format= outputformat.createprettyprint ();//Format

Document.setxmlencoding ("UTF-8"); Setting the XML encoding format

XMLWriter XMLWriter = new XMLWriter (new FileOutputStream (fileName), format);

Xmlwriter.write (document);

Xmlwriter.close ();

This article is from the "7854772" blog, please be sure to keep this source http://7864772.blog.51cto.com/7854772/1733931

Read and write XML files using dom4j

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.