Java generates XML files

Source: Internet
Author: User

Our data in the database can be extracted to generate an XML file for easy transmission. For example, the database has the admin this table:

We write a Java class that represents the admin data:

1  PackageXmldom.vo;2  3 Importjava.io.Serializable;4 Importjava.util.Date;5 Importjava.util.List;6 7  Public classAdminImplementsSerializable8 {9     PrivateString aid;Ten     PrivateString password; One     PrivateInteger rid; A     PrivateInteger type; -     PrivateDate lastdate; -     PrivateInteger Flag; the       -      -   -   +      PublicInteger Getrid () -     { +         returnrid; A     } at      Public voidSetrid (Integer rid) -     { -          This. RID =rid; -     } -      PublicString getaid () -     { in         returnaid; -     } to      Public voidSetaid (String aid) +     { -          This. AID =aid; the     } *      PublicString GetPassword () $     {Panax Notoginseng         returnpassword; -     } the      Public voidSetPassword (String password) +     { A          This. Password =password; the     } +      PublicInteger GetType () -     { $         returntype; $     } -      Public voidsetType (Integer type) -     { the          This. Type =type; -     }Wuyi      PublicDate getlastdate () the     { -         returnlastdate; Wu     } -      Public voidsetlastdate (Date lastdate) About     { $          This. lastdate =lastdate; -     } -      PublicInteger Getflag () -     { A         returnFlag; +     } the      Public voidSetflag (Integer flag) -     { $          This. Flag =Flag; the     }  the      the}

The data in the table is then converted into a linked list through JDBC:

1  PackageXmldom.dbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.SQLException;6 7  Public classdatabaseconnection8 {9     Private Static FinalString mysqldriver = "Org.gjt.mm.mysql.Driver";Ten     Private Static FinalString Mysqlurl = "Jdbc:mysql://localhost:3306/hrdb"; One     Private Static FinalString USERNAME = "root"; A     Private Static FinalString PASSWORD = "admin"; -  -     PrivateConnection Conn; the  -      Publicdatabaseconnection () -     { -         Try +         { - Class.forName (mysqldriver); +              This. conn =drivermanager.getconnection (Mysqlurl, USERNAME, PASSWORD); A}Catch(Exception e) at         { - e.printstacktrace (); -         } -     } -  -      PublicConnection getconnection ()throwsException in     { -         if(NULL!= This. Conn) to             return  This. Conn; +         Else -             Throw NewException ("Get Database connection Failed"); the     } *  $      Public voidClose ()Panax Notoginseng     { -         if(NULL!= This. Conn) the         { +             Try A             { the                  This. Conn.close (); +}Catch(SQLException e) -             { $ e.printstacktrace (); $             } -         } -     } the}
     Public StaticList<admin> Loadfromdb ()throwsException {List<Admin> Alladmins =NewArraylist<admin>(); DatabaseConnection DBC=Newdatabaseconnection (); Connection Conn=dbc.getconnection (); PreparedStatement PS= Conn.preparestatement ("Select Aid,rid,type,lastdate,flag from Admin"); ResultSet RS=Ps.executequery ();  while(Rs.next ()) {Admin Vo=NewAdmin (); Vo.setaid (Rs.getstring ("Aid")); Vo.setrid (Rs.getint ("RID")); Vo.settype (Rs.getint ("Type")); Vo.setlastdate (Rs.getdate ("Lastdate")); Vo.setflag (Rs.getint ("Flag"));        Alladmins.add (VO); }                returnAlladmins; }

The linked list is then converted into an in-memory XML structure:

     Public StaticDocument getdocument (list<admin> alladmins)throwsException {documentbuilderfactory dbfactory=documentbuilderfactory.newinstance (); Documentbuilder Documentbuilder=Dbfactory.newdocumentbuilder (); Document Document=documentbuilder.newdocument (); Element Root= document.createelement ("admins");  for(admin item:alladmins) {Element Admin= Document.createelement ("admin"); Admin.setattribute ("Aid", Item.getaid ()); Element RID= document.createelement ("RID");                        Rid.appendchild (document.createTextNode (string.valueof (Item.getrid ())); Element type= Document.createelement ("type");                        Type.appendchild (document.createTextNode (string.valueof (Item.gettype ())); Element lastdate= Document.createelement ("Lastdate");                        Lastdate.appendchild (document.createTextNode (string.valueof (Item.getlastdate ())); Element Flag= Document.createelement ("Flag");                        Flag.appendchild (document.createTextNode (string.valueof (Item.getflag ()));            Admin.appendchild (RID);            Admin.appendchild (type);            Admin.appendchild (lastdate);                        Admin.appendchild (flag);        Root.appendchild (admin);                                } document.appendchild (root); returndocument; }

Then save the XML for that memory to Local:

     Public Static voidMain (string[] args)throwsException {List<Admin> Alladmins =Loadfromdb (); Document Document=getdocument (alladmins); Source Source=NewDomsource (document); Streamresult Stream=NewStreamresult (NewFileOutputStream (NewFile ("C:\\d\\code\\resource\\admins.xml"))); Transformerfactory transfactory=transformerfactory.newinstance (); Transformer Trans=Transfactory.newtransformer (); Trans.setoutputproperty (outputkeys.encoding,"UTF-8");                Trans.transform (source, stream); System.out.println ("Main done//~~"); }

Java generates XML files

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.