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