Xml parsing: using dom4j APIs to perform CRUD on xml files (2)

Source: Internet
Author: User
Use dom4j APIs to perform CRUD (1) on the xml file. for details, see blog. csdn. NETqq_32059827articledetails51524330. Use dom4j APIs to perform CRUD (1) on the xml file, see blog #, and perform another exercise.


Package gz. itcast. a_dom4j_write; import java. io. file; import java. io. fileOutputStream; import java. util. iterator; import org. dom4j. document; import org. dom4j. export enthelper; import org. dom4j. element; import org. dom4j. io. outputFormat; import org. dom4j. io. SAXReader; import org. dom4j. io. XMLWriter; import org. junit. test;/*** classroom exercise: * 1. use dom4j APIs to generate the following xml files
 
  
   
    
Zhang San
   
   
    
Male
   
   
    
Computer class 1
   Guangzhou Tianhe District
  
  
   
    
Li Si
   
   
    
Female
   
   
    
Computer class 2
   Guangzhou Yuexiu District
  
 2. change the name of the student whose id is 2 to "Wang Li. delete student * @ author APPle **/public class Demo4 {/*** 1 with id 2. generate the specified xml document * @ throws Exception */@ Testpublic void test1 () throws Exception {// 1. memory creation xml Document doc = incluenthelper. createDocument (); // 2. write content. the first write is the root tag Element rootElem = doc. addElement ("Students"); // 2.1 add the label Element studentElem1 = rootElem. addElement ("Student"); // 2.2 add the attribute studentElem1.addAttribute ("id", "1"); // 2.3 add a tag And set the text studentElem1.addElement ("name "). setText ("zhang san"); studentElem1.addElement ("gender "). setText ("male"); studentElem1.addElement ("grade "). setText ("computer class 1"); studentElem1.addElement ("address "). setText ("Guangzhou Tianhe"); // 2.1 add the label Element studentElem2 = rootElem. addElement ("Student"); // 2.2 add the studentElem2.addAttribute ("id", "2"); // 2.3 add a label and set the text studentElem2.addElement ("name "). setText ("Li Si"); studentElem2.addElemen T ("gender "). setText ("female"); studentElem2.addElement ("grade "). setText ("computer class 2"); studentElem2.addElement ("address "). setText ("Guangzhou Yuexiu"); // 3. write all the changes to the doc once. Write the content to the xml file // 3.1 output location FileOutputStream out = new FileOutputStream ("d:/student. xml "); // 3.2 specify the format OutputFormat format = OutputFormat. createPrettyPrint (); // sets the encoding format. setEncoding ("UTF-8"); XMLWriter writer = new XMLWriter (out, format); // 3.3 write the content writer. write (doc); // 3.4 close the resource writer. close ();}/*** 2. change student name * @ throws Exception */@ Testpublic void test2 () throws Exception {// 1. the student Document doc = new SAXReader () with id 2 is found (). read (new File ("d:/student. xml "); // 1.1 find all Student labels Iterator
 
  
It = doc. getRootElement (). elementIterator ("Student"); while (it. hasNext () {Element stuElem = it. next (); // Student tag // 1.2 query if (stuElem. attributeValue ("id "). equals ("2") {stuElem. element ("name "). setText ("Wang Li"); // overwrite break ;}// 3.1 output position FileOutputStream out = new FileOutputStream ("d:/student. xml "); // 3.2 specify the format OutputFormat format = OutputFormat. createPrettyPrint (); // sets the encoding format. setEncoding ("UTF-8"); XMLWriter writer = new XMLWriter (out, format); // 3.3 write the content writer. write (doc); // 3.4 close the resource writer. close ();}/*** 3. delete student * @ throws Exception */@ Testpublic void test3 () throws Exception {// 1. the student Document doc = new SAXReader () with id 2 is found (). read (new File ("e:/student. xml "); // 1.1 find all Student labels Iterator
  
   
It = doc. getRootElement (). elementIterator ("Student"); while (it. hasNext () {Element stuElem = it. next (); // 1.2 query if (stuElem. attributeValue ("id "). equals ("2") {// 1.3 Delete this student tag stuElem. detach (); // suicide break;} // 3.1 output position FileOutputStream out = new FileOutputStream ("e:/student. xml "); // 3.2 specify the format OutputFormat format = OutputFormat. createPrettyPrint (); // sets the encoding format. setEncoding ("UTF-8"); XMLWriter writer = new XMLWriter (out, format); // 3.3 write the content writer. write (doc); // 3.4 close the resource writer. close ();}}
  
 


The above is the content of CRUD (2) for xml files using the dom4j api for xml parsing. For more information, see PHP Chinese network (www.php1.cn )!

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.