Parse XML files

Source: Internet
Author: User
Package com.cn. javase. dom4j; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. SQL. connection; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import Java. util. date; import Java. util. iterator; import Java. util. list; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. documenthe Lper; import Org. dom4j. element; import Org. dom4j. node; import Org. dom4j. io. outputformat; import Org. dom4j. io. saxreader; import Org. dom4j. io. xmlwriter; public class writexml extends basedao {/*** @ Param ARGs * @ throws sqlexception * @ throws classnotfoundexception * @ throws ioexception */private connection conn; public static void main (string [] ARGs) throws sqlexception, classnotfoundexception {// todo auto-G Enerated method stub // new writexml (). writexmlfile ("D: // person. XML "); New writexml (). readxmlfile ("D: // person. XML ");}/*** @ return defines a method for reading data from the database and saving the dataset to the XML file; * @ throws classnotfoundexception * @ throws sqlexception */Public int writexmlfile (string file) throws classnotfoundexception, sqlexception {conn = This. getconn (); statement ST = NULL; try {ST = Conn. createstatement ();} catch (sqlexcepti On E1) {// todo auto-generated Catch Block e1.printstacktrace ();} string SQL = "select * From person"; resultset rs = st.exe cutequery (SQL ); document document = incluenthelper. createdocument (); // format file output; document. setxmlencoding ("gb2312"); Date = new date (); string S = date. tolocalestring (); int value = 0; // mark whether the file is successfully written; // Add a comment for XML; document. addcomment ("reads data from the table" person "in the Database" meemei "to" person. XM L file! Modified on: "+ S); element root = document. addelement ("meemei"); outputformat format = outputformat. createprettyprint (); format. setencoding ("gb2312"); While (RS. next () {// read data from the database integer id = Rs. getint (1); string user = Rs. getstring (2); float salary = Rs. getfloat (3); integer age = Rs. getint (4); string dept1 = Rs. getstring (5); system. out. println (ID + User); element = root. addelement ("person "); Element id = element. addelement ("ID"); element pname = element. addelement ("pname"); element psalary = element. addelement ("psalary"); element page = element. addelement ("page"); element dept = element. addelement ("Dept"); // put the data read from the database into XML; id. addtext (string. valueof (ID); pname. addtext (User); psalary. addtext (string. valueof (salary); page. addtext (string. valueof (AGE); Dept. addtext (dept1); xmlwrit Er writer; // construct the file output stream; try {writer = new xmlwriter (New filewriter (new file (File), format); writer. write (document); writer. close (); value = 1; // If execution succeeds, 1 is returned;} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}} return value;}/*** read data from an XML file and save it to the database. ** @ return */Public int readxmlfile (string file) {conn = This. getconn (); saxreader reader = new saxreader (); int va Lue = 0; try {document DOC = reader. read (new file (File); element = Doc. getrootelement (); system. out. println ("root node:" + element. getname (); // obtain the root node list <element> List = element. elements (); iterator <element> it = List. iterator (); string database = element. getname (); system. out. println ("Database Name:" + database); string table = ""; integer id = 0; string pname = ""; float psalary = 0.0f; integer Pa GE = 0; string dept = ""; while (it. hasnext () {Element E = it. next (); system. out. println ("-------------------------------------"); system. out. println ("table name:" + E. getname (); table = E. getname (); iterator itor = E. elementiterator (); While (itor. hasnext () {// ------------- ID ------------ node idnode = (node) itor. next (); string idname = idnode. getname (); string idvalue = idnode. gettext (); Id = integer. valueof (idvalue); system. out. println ("node:" + idname + "----> value:" + idvalue); // ------------ pname ------------- node namenode = (node) itor. next (); string namename = namenode. getname (); string namevalue = namenode. gettext (); pname = namevalue; system. out. println ("node:" + namename + "----> value:" + namevalue); // ------------- psalary ------------- node salarynode = (node) itor. next (); string salaryname = Salarynode. getname (); string salaryvalue = salarynode. gettext (); psalary = float. valueof (salaryvalue); system. out. println ("node:" + salaryname + "----> value:" + salaryvalue); // ------------ page -------------- node agenode = (node) itor. next (); string agename = agenode. getname (); string agevalue = agenode. gettext (); page = integer. valueof (agevalue); system. out. println ("node:" + agename + "----> value:" + agevalue );// -------------- Dept -------------- node deptnode = (node) itor. next (); string deptname = deptnode. getname (); string deptvalue = deptnode. gettext (); dept = deptvalue; system. out. println ("node:" + deptname + "----> value:" + deptvalue ); string SQL = "insert into" + Table + "values (?,?,?,?,?) "; Try {preparedstatement PS = Conn. preparestatement (SQL); PS. setint (1, ID); PS. setstring (2, pname); PS. setfloat (3, psalary); PS. setint (4, page); PS. setstring (5, Dept); ps.exe cuteupdate ();} catch (sqlexception E1) {// todo auto-generated Catch Block e1.printstacktrace ();} system. out. println (SQL) ;}} catch (incluentexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return value ;}}

 

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.