Using dom4j to write XML files-source code

Source: Internet
Author: User

DOM4J Version: 2.1.1

To generate XML document steps using DOM4J:

1. Create a blank document for the Documents object.

2. Add the root element to the document, the element returned represents the root, and the XML document has only one root element.

3. Add child elements.

4. Create a XmlWriter object

5. Output Document Object

Element Object

1  Packagecnblogs.testcsv;2 3  Public classPerson {4     PrivateString ID;5     PrivateString name;6     PrivateString sex;7     Private intAge ;8 9      PublicPerson () {Ten     } One  A      PublicPerson (string ID, string name, String sex,intAge ) { -          This. ID =ID; -          This. Name =name; the          This. Sex =sex; -          This. Age =Age ; -     } -  +      PublicString getId () { -         returnID; +     } A  at      Public voidsetId (String id) { -          This. ID =ID; -     } -  -      PublicString GetName () { -         returnname; in     } -  to      Public voidsetName (String name) { +          This. Name =name; -     } the  *      PublicString Getsex () { $         returnsex;Panax Notoginseng     } -  the      Public voidsetsex (String sex) { +          This. Sex =sex; A     } the  +      Public intGetage () { -         returnAge ; $     } $  -      Public voidSetage (intAge ) { -          This. Age =Age ; the     } -}

Write XML

1  PackageCnblogs.testxml;2 3 ImportCnblogs.testCSV.Person;4 Importorg.dom4j.Document;5 ImportOrg.dom4j.DocumentHelper;6 Importorg.dom4j.Element;7 ImportOrg.dom4j.io.OutputFormat;8 ImportOrg.dom4j.io.XMLWriter;9 Ten ImportJava.io.FileOutputStream; One Importjava.util.ArrayList; A Importjava.util.List; - ImportJava.util.UUID; -  the  Public classFilexml { -     Private Static FinalString fileName = "D:\\workspace\\tmp\\obj.xml"; -  -     /** + * Generate UUID -      * +      * @return32-bit UUID A      */ at     Private StaticString getUUID32 () { -         returnUuid.randomuuid (). toString (). Replace ("-", ""). toLowerCase (); -     } -  -     /** - * Construction Data in      * -      * @returnData to      */ +     Private StaticList<person>Builddata () { -List<person> personlist =NewArraylist<person> (10); thePersonlist.add (NewPerson (getUUID32 (), "Zhang San", "female", 26)); *Personlist.add (NewPerson (getUUID32 (), "John Doe", "man", 34)); $Personlist.add (NewPerson (getUUID32 (), "Harry", "female", 55));Panax NotoginsengPersonlist.add (NewPerson (getUUID32 (), "One by One", "female", 11)); -         returnpersonlist; the     } +  A      Public Static voidwriteXml () { theList<person> personlist =Builddata (); +Document doc =documenthelper.createdocument (); -Element root = Doc.addelement ("Personlist"); $          for(person emp:personlist) { $Element Empele = root.addelement ("Person"); -Element Nameele = empele.addelement ("id"); - Nameele.addtext (Emp.getid ()); theElement Ageele = empele.addelement ("name"); - Ageele.addtext (Emp.getname ());WuyiElement Genderele = empele.addelement ("Sex"); the Genderele.addtext (Emp.getsex ()); -Element Salele = empele.addelement ("Age"); WuSalele.addtext (Emp.getage () + ""); -Empele.addattribute ("id", Emp.getid () + "");//adds a property of the specified name and corresponding value to the current element About         } $         Try { -XMLWriter writer =NewXMLWriter (Outputformat.createprettyprint ()); -FileOutputStream fos =NewFileOutputStream (fileName); - Writer.setoutputstream (FOS); A Writer.write (DOC); + writer.close (); the         } -         Catch(Exception e) { $ e.printstacktrace (); the         } the     } the  the     /** -      * @paramargs in      */ the      Public Static voidMain (string[] args)throwsException { the writeXml (); About     } the}

Output results

Using dom4j to write XML files-source code

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.