Dom4j (2) Editing

Source: Internet
Author: User

The fund information fund. XML is stored in the project root directory:

<? XML version = "1.0" encoding = "UTF-8"?> <Funds> <fund id = "0"> <Name> Huaxia dashboard </Name> <code> 000011 </code> <type> hybrid </type> <URL> http://jingzhi.funds.hexun.com/000011.shtml </URL> </Fund> <fund id = "1"> <Name> Hua 'an Hongli </Name> <code> 040005 </code> <type> stock type </type> <URL> http://jingzhi.funds.hexun.com/040005.shtml </URL> </Fund> </funds>

ID or an uppercase letter "ID" as attribute.

Construct a corresponding Java Bean:

package leon.corejava.model;public class Fund {private String name;private String code;private String url;private String type;public String toString(){return this.code+"\t"+this.name+"\t"+this.url;}//getter and setter//...}

Test class:

Package Leon. corejava. XML; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Leon. corejava. model. fund; import Org. dom4j. document; import Org. dom4j. element; import Org. dom4j. io. outputformat; import Org. dom4j. io. saxreader; import Org. dom4j. io. xmlwriter; public class fundxmltest {public static final string id = "ID"; public static void main (string [] ARGs) throws exception {file xmlfile = new file ("fund. XML "); saxreader reader = new saxreader (); document DOC = reader. read (xmlfile); element root = Doc. getrootelement (); List <fund> fundlist = new arraylist <fund> (); int I = 0; For (iterator iter = root. elementiterator (); ITER. hasnext ();) {element = (element) ITER. next (); fundlist. add (elementtofund (element); I ++;} For (Fund F: fundlist) {system. out. println (f);} // modify elementelement E = root. elementbyid ("0"); E. element ("name "). settext (""); // Add new elementelement newfund = root. addelement ("fund"); newfund. addattribute (ID, String. valueof (I); newfund. addelement ("name "). settext ("CITIC double benefit"); newfund. addelement ("Code "). settext ("288102"); newfund. addelement ("url "). settext ("http://jingzhi.funds.hexun.com/288102.shtml"); write (Doc, new file ("new_fund.xml");} public static fund elementtofund (Element E) {fund F = new fund (); F. setcode (E. element ("Code "). gettexttrim (); F. setname (E. element ("name "). gettexttrim (); F. seturl (E. element ("url "). gettexttrim (); F. settype (E. element ("type "). gettexttrim (); Return F;} public static void write (document, file F) throws ioexception {// format xmloutputformat format = outputformat. createprettyprint (); // format short XML // format = outputformat. createcompactformat (); // not format // xmlwriter writer = new xmlwriter (New filewriter (f); // format. setencoding ("GBK"); // formatxmlwriter writer = new xmlwriter (New filewriter (F), format); writer. write (document);/* // format short xmlformat = outputformat. createcompactformat (); writer = new xmlwriter (system. out, format); */writer. close ();}}

Create a new file new_fund.xml in the project root directory.

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.