Java dom4j Elementiterator

Source: Internet
Author: User

Student.xml

<Students>   <StudentID= "1">     <name>Tom</name>      <Gender>Man</Gender>      <Grade>Computer Class One</Grade>      <Address>Guangzhou</Address>   </Student>    <StudentID= "2">     <name>Wang Sweet</name>      <Gender>Man</Gender>      <Grade>Computer Class One</Grade>      <Address>Guangzhou</Address>   </Student> </Students>
Document doc= documenthelper.createdocument (); Element Root =doc.addelement ("students"); Element student1=   root.addelement ("Student"). AddAttribute ("id", "1"); Student1.addelement ("name"). SetText ("Zhang San "); Student1.addelement (" Gender "). SetText (" male "); Student1.addelement (" Grade "). SetText (" one class computer "); Student1.addelement ("Address"). SetText ("Guangzhou"); Element studentElem2 = root.addelement ("Student"),//2.2 Add attribute studentelem2.addattribute ("id", "2"),//2.3 add tag, Set the text studentelem2.addelement ("name") at the same time. SetText ("John Doe"); Studentelem2.addelement ("Gender"). SetText ("female"); Studentelem2.addelement ("Grade"). SetText ("Computer Class 2") studentelem2.addelement ("Address"). SetText ("Guangzhou Yuexiu"); FileOutputStream  out= New FileOutputStream ("E:/student.xml"); OutputFormat format= Outputformat.createprettyprint (); Format.setencoding ("Utf-8"); XMLWriter  writer= New XMLWriter (out, format); Writer.write (doc); Writer.close ();

  

1         //1. Check with the student with ID 2 and modify the student! 2Document doc =NewSaxreader (). Read (NewFile ("E:/student.xml")); 3Element root =doc.getrootelement ();4Iterator<element> it =root.elementiterator ();5 6             while(It.hasnext ()) {7Element e =It.next ();8                   if(E.attributevalue ("id"). Equals ("2"))){9             TenE.element ("name"). SetText ("Wang Sweet"); One                      Break; A                 } -            } -  the         //3.1 Output Position -FileOutputStream out =NewFileOutputStream ("E:/student.xml"); -         //3.2 Specifying the format -OutputFormat format =outputformat.createprettyprint (); +         //Set Encoding -Format.setencoding ("Utf-8"); +XMLWriter writer =NewXMLWriter (Out,format); A         //3.3 Writing content at Writer.write (DOC); -         //3.4 Closing Resources - writer.close (); -}

1Document doc =NewSaxreader (). Read (NewFile ("E:/student.xml")); 2Element root =doc.getrootelement ();3Iterator<element> it = root.elementiterator ("Student");4 5             while(It.hasnext ()) {6Element e =It.next ();7                   if(E.attributevalue ("id"). Equals ("2"))){8             9E.element ("name"). SetText ("Wang Ba"));Ten                      Break; One                 } A}

Java dom4j elementiterator

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.