How does dom4j add, delete, modify, and create xml files?

Source: Internet
Author: User

There are many ways to operate xml. Here we mainly talk about the xml Parsing Method of dom4j. jar and some common operations. The source code is very simple.

Create xml:

After the information is inserted:

Vcy92rXjoaM8L3A + CjxwPtDeuMTQxc + ivdjNvKO6PC9wPgo8cD48aW1nIHNyYz0 = "http://www.bkjia.com/uploadfile/Collfiles/20140107/20140107095057194.jpg" alt = "\">

Removal Information:

The above is a simple creation of the entire xml implemented by the Code. The Code is simple and never be surprised. For more information, see code comments.

Package trs. li. parsexml; import java. io. file; import java. io. fileOutputStream; import java. util. iterator; import java. util. list; import org. dom4j. attribute; import org. dom4j. document; import org. dom4j. export enthelper; import org. dom4j. element; import org. dom4j. io. SAXReader; import org. dom4j. io. XMLWriter;/** Parse xml file * @ author trsli * this class is mainly for personal simple learning dom4j. jar's xml parsing method is not practical. * node depth increases the difficulty to some extent. Here I use two layers, which is a big failure. **/Public class ParseXMLDemo {public static void main (String [] args) {try {// createXML (); // 1 create // insertXML (); // 2 insert information // modifyXML (); // 3 modify delXML (); // 4 remove} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace () ;}/ ** create xml file * Note: I made an error at the beginning and later I realized that only the following node passed the document. * created by addElement () * because the operation method is similar to the javascript DOM operation, learning to parse xml is good for learning javascript **/private static void createXML () throws Exception {String filename = "E: \ book. xml "; Document document = incluenthelper. createDocument (); Element element = document. addElement ("books"); element. addAttribute ("id", "books"); element. addComment ("this is a line of comment"); Element celement = element. addElement ("book"); celement. addComment ("this is a node"); celement. addattriement ("value", "novel"); celement. setText ("Dream of Red Mansions"); // element. add (celement); Element element1 = element. addElement ("book"); element1.addattriement ("value", "technology"); element1.setText ("C ++"); // element. add (element1); Element element2 = element. addElement ("book"); element2.addattriement ("value", ""); element2.setText (""); // element. add (element2); XMLWriter xmlWriter = new XMLWriter (new FileOutputStream (new File (filename); xmlWriter. write (document); System. out. println ("created successfully... "); xmlWriter. close ();}/** Insert new data into xml * addComment () add comments **/private static void insertXML () throws Exception {Document document = null; SAXReader reader = new SAXReader (); document = reader. read ("E: \ book. xml "); Element element = document. getRootElement (); // get the root node // Element ele = element. element ("books"); Element book = element. addElement ("book"); book. addComment ("this is APPEND"); book. addAttribute ("value", "Science and Education"); book. setText ("Star Wars"); XMLWriter xmlWriter = new XMLWriter (new FileOutputStream (new File ("E: \ book. xml "); xmlWriter. write (document); System. out. println ("insert successful .... ");}/** modify xml text * Here we mainly modify attributes and node text information **/private static void modifyXML () throws Exception {Document document = null; SAXReader reader = new SAXReader (); document = reader. read ("E: \ book. xml "); List list = document. selectNodes ("/books/book/@ value"); // search for the Iterator it = list attribute. iterator (); while (it. hasNext () {Attribute attr = (Attribute) it. next (); // property traversal if (attr. getValue (). equals ("Science and Education") {System. out. println ("start to modify .... "); attr. setValue ("sci-fi novels") ;}/// search node List list1 = document. selectNodes ("/books/book"); Iterator it1 = list1.iterator (); while (it1.hasNext () {Element element = (Element) it1.next (); // node traversal if (element. getText (). equals ("") {System. out. println ("start to modify 2 ..... "); element. setText ("Mathematics") ;}} XMLWriter xmlwriter = new XMLWriter (new FileOutputStream ("E: \ book. xml "); xmlwriter. write (document); System. out. println ("completed .... "); xmlwriter. close ();}/** Delete xml content * If a deeper node needs to be deleted, you can use multi-layer traversal **/private static void delXML () throws Exception {Document document = null; SAXReader reader = new SAXReader (); document = reader. read ("E: \ book. xml "); Element root = document. getRootElement (); List list = document. selectNodes ("/books/book"); Iterator it = list. iterator (); while (it. hasNext () {Element element = (Element) it. next (); if (element. getText (). equals ("Mathematics") {System. out. println ("delete... "); root. remove (element) ;}} XMLWriter xmlwriter = new XMLWriter (new FileOutputStream ("E: \ book. xml "); xmlwriter. write (document); System. out. println ("deleted... "); xmlwriter. close ();}}


The article is complete.

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.