Java dom4j Create, modify, and delete XML file contents

Source: Internet
Author: User

ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;ImportOrg.dom4j.Attribute;Importorg.dom4j.Document;Importorg.dom4j.DocumentException;ImportOrg.dom4j.DocumentHelper;Importorg.dom4j.Element;ImportOrg.dom4j.io.OutputFormat;ImportOrg.dom4j.io.SAXReader;ImportOrg.dom4j.io.XMLWriter;Importorg.junit.Test; Public classWriter3 {@Test

/**
Creating XML node Information

*/ Public voidmodify1 () {Document doc=documenthelper.createdocument (); Element Rootelem=doc.addelement ("ContactList"); Element Contactelem= Rootelem.addelement ("Contact")); Contactelem.addattribute ("id", "001"). AddAttribute ("name", "Eric"); Writes the attribute value directly on the node contactelem.addelement ("Name"). AddText ("Zhang San")); Add label name content is text contactelem.addelement ("Age"). AddText ("20"); Add tag age content belongs to text//Whether it is created, modified, or deleted after you write to the file FileOutputStream out=NewFileOutputStream ("E:/contact2.xml"); OutputFormat format=Outputformat.createprettyprint (); Format.setencoding ("Utf-8"); XMLWriter writer=NewXMLWriter (Out,format); Writer.write (DOC); Writer.close ();

The creation effect is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><contactlist> <contact   id= "001" Name= "Eric" >     < name>jimi</name>      <age>20</age>   </contact> </contactList>

To modify a code snippet:

@Test Public voidModify3 ()throwsException {//modify this three lines of code Document doc=NewSaxreader (). Read (NewFile ("E:/contact2.xml")); Element name=doc.getrootelement (). Element ("contact"). Element ("name"); Name.settext ("Jimi");

FileOutputStream out=NewFileOutputStream ("E:/contact2.xml"); OutputFormat format=Outputformat.createprettyprint (); Format.setencoding ("Utf-8"); XMLWriter writer=NewXMLWriter (Out,format); Writer.write (DOC); Writer.close ();}

  

Java dom4j Create, modify, and delete XML file contents

Related Article

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.