// Readfrdata. Java
Package com. Xie. xmlparse. dom4j;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. util. arraylist;
Import java. util. List;
// Read data from the database
Import com. Xie. conndb. conndb;
Import com. Xie. xmlparse. dom4j. Modal. student;
Public class readfrdata {
Private list <student> Al = NULL;
Public list <student> getal (){
Return getdatafrommysql ();
}
/**
* Read data from the database, put it into student, and then put student into the list container
* @ Author Center
* @ Return returns a list container.
*/
Private list <student> getdatafrommysql (){
Try {
Al = new arraylist <student> ();
Connection Ct = new conndb (). getconnstu ();
Preparedstatement PS = CT. preparestatement ("select studentinfo. *, class. claid from studentinfo, class where studentinfo. stuid = Class. stuid ");
Resultset rsw.ps.exe cutequery ();
While (Rs. Next ()){
Student s = new student ();
S. setclassid (Rs. getlong (5 ));
S. setstuid (Rs. getlong (1 ));
S. setstuname (Rs. getstring (2 ));
S. setstusex (Rs. getstring (3 ));
S. setstuage (Rs. getint (4 ));
Al. Add (s );
}
} Catch (exception e ){
System. Out. println ("getdatafrommysql exception ");
E. printstacktrace ();
}
Return al;
}
}
// Inserttoxml. Java
Package com. Xie. xmlparse. dom4j;
Import java. Io. file;
Import java. Io. filewriter;
Import java. Io. ioexception;
Import java. util. hashset;
Import java. util. iterator;
Import java. util. List;
Import java. util. Set;
Import org. dom4j. Document;
Import org. dom4j. documenthelper;
Import org. dom4j. element;
Import org. dom4j. Io. outputformat;
Import org. dom4j. Io. xmlwriter;
Import com. Xie. xmlparse. dom4j. Modal. student;
// Generate an XML file for the data read from the database
Public class inserttoxml {
Private set <long> hset = NULL;
/**
* @ Athor Center
* @ Param pathname string name of the input file + file name
* @ Return B Return true if the XML file is successfully generated; otherwise, false is returned.
*/
Public Boolean createxml (string pathname ){
Boolean B = false;
Document Doc = incluenthelper. createdocument (); // create a document
Element schoolele = Doc. addelement ("school"); // Add a root element
Schoolele. addcomment ("the root School of the document has been created. "); // Add comments
Hset = new hashset <long> ();
List <student> Al = new readfrdata (). getal ();
For (INT I = 0; I <Al. Size (); I ++ ){
Student s = Al. Get (I );
Hset. Add (S. getclassid ());
}
For (iterator <long> it = hset. iterator (); it. hasnext ();){
Long classid = it. Next ();
Element classele = schoolele. addelement ("class ");
Classele. addattribute ("name", classid + "");
For (INT I = 0; I <Al. Size (); I ++ ){
Student s = Al. Get (I );
If (classid. Equals (S. getclassid ())){
Element studentele = classele. addelement ("student ");
Studentele. addelement ("stuid"). addtext (S. getstuid () + "");
Studentele. addelement ("stuname"). addtext (S. getstuname ());
Studentele. addelement ("stusex"). addtext (S. getstusex ());
Studentele. addelement ("stuage"). addtext (S. getstuage () + "");
}
}
}
Try {
/* View the help document by yourself
* Outputformat format = new outputformat ("", true, "gb2312 ");
*/
/*
* Create a beautiful outputformat
* You can use setencoding to set the transmission string. The default value is UTF-8.
*/
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("gb2312 ");
/* Create outputformat
Format = outputformat. createcompactformat ();
*/
Xmlwriter writer = new xmlwriter (New filewriter (new file (pathname), format );
Writer. Write (DOC );
Writer. Close ();
B = true;
} Catch (ioexception e ){
E. printstacktrace ();
}
Return B;
}
Public static void main (string [] ARGs ){
String pathname = "D: // project // xmlparse // xmlfiles // center. xml ";
New inserttoxml (). createxml (pathname );
}
}
Source XML and generated XML: encoding changed, MySQL database encoding for gb2312, source XML encoding for ut-8, when written to the database, the Chinese in the database is not correctly displayed, however, the Chinese characters in the database can be correctly read by the program. When you use outputformat to write XML, the default string is UTF-8, and the Chinese characters in the generated XML file are garbled. The outputformat encoding is changed to gb2312 for correct display.
// Student. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<School>
<Class name = "030713">
<Student>
<Stuid> 03071300 </stuid>
<Stuname> James </stuname>
<Stusex> male </stusex>
<Stuage> 10 </stuage>
</Student>
<Student>
<Stuid> 03071301 </stuid>
<Stuname> Xiaohua </stuname>
<Stusex> female </stusex>
<Stuage> 20 </stuage>
</Student>
<Student>
<Stuid> 03071302 </stuid>
<Stuname> unknown </stuname>
<Stusex> male </stusex>
<Stuage> 15 </stuage>
</Student>
</Class>
<Class name = "030714">
<Student>
<Stuid> 03071400 </stuid>
<Stuname> James </stuname>
<Stusex> male </stusex>
<Stuage> 10 </stuage>
</Student>
<Student>
<Stuid> 03071401 </stuid>
<Stuname> Xiaohua </stuname>
<Stusex> female </stusex>
<Stuage> 20 </stuage>
</Student>
<Student>
<Stuid> 03071402 </stuid>
<Stuname> unknown </stuname>
<Stusex> male </stusex>
<Stuage> 15 </stuage>
</Student>
</Class>
</School>
// Center. xml
<? XML version = "1.0" encoding = "gb2312"?>
<School>
<! -- The root School of the document has been created. -->
<Class name = "30714">
<Student>
<Stuid> 3071400 </stuid>
<Stuname> James </stuname>
<Stusex> male </stusex>
<Stuage> 10 </stuage>
</Student>
<Student>
<Stuid> 3071401 </stuid>
<Stuname> Xiaohua </stuname>
<Stusex> female </stusex>
<Stuage> 20 </stuage>
</Student>
<Student>
<Stuid> 3071402 </stuid>
<Stuname> unknown </stuname>
<Stusex> male </stusex>
<Stuage> 15 </stuage>
</Student>
</Class>
<Class name = "30713">
<Student>
<Stuid> 3071300 </stuid>
<Stuname> James </stuname>
<Stusex> male </stusex>
<Stuage> 10 </stuage>
</Student>
<Student>
<Stuid> 3071301 </stuid>
<Stuname> Xiaohua </stuname>
<Stusex> female </stusex>
<Stuage> 20 </stuage>
</Student>
<Student>
<Stuid> 3071302 </stuid>
<Stuname> unknown </stuname>
<Stusex> male </stusex>
<Stuage> 15 </stuage>
</Student>
</Class>
</School>