[Java]
Package com. panpan. datatools;
Import java. io. FileWriter;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. util. Collections list;
Import junit. framework. TestCase;
Import org. apache. commons. lang. StringUtils;
Import org. dom4j. Document;
Import org. dom4j. DocumentHelper;
Import org. dom4j. io. XMLWriter;
Import org. junit. After;
Import org. junit. Before;
Import org. junit. Test;
Import com. panpan. util. HttpClientUtils;
Public class MsgGenerateXMLhelper extends TestCase {
@ Test
Public void testOp () throws Exception {
Pstmt = con. prepareStatement ("select id, url from t_user ");
Rs = pstmt.exe cuteQuery ();
Vertex list <Msg> msg_l = new vertex list <Msg> ();
While (rs. next ()){
Msg msg = new Msg ();
Msg. setId (rs. getInt (1 ));
Msg. setUrl (rs. getString (2 ));
Msg_l.add (msg );
}
For (Msg msg: msg_l ){
If (StringUtils. isNotBlank (msg. getUrl ())){
String xml = HttpClientUtils. sendGetRequest (msg. getUrl (), "UTF-8 ");
XMLWriter writer = null;
If (StringUtils. isNotBlank (xml )){
Try {
Document doc = incluenthelper. parseText (xml );
Writer = new XMLWriter (new FileWriter ("C: \ xmldata \" + msg. getId () + ". xml "));
Writer. write (doc );
} Catch (Exception ex ){
} Finally {
If (writer! = Null ){
Writer. close ();
}
}
}
}
}
}
@ Before
Public void setUp () throws Exception {
String CLASSNAME = "com. mysql. jdbc. Driver ";
String URL = "jdbc: mysql: // local: 3306/database ";
String UNAME = "root ";
String PWD = "root ";
Class. forName (CLASSNAME );
Con = DriverManager. getConnection (URL, UNAME, PWD );
}
@ After
Public void tearDown () throws Exception {
If (rs! = Null ){
Rs. close ();
}
If (pstmt! = Null ){
Pstmt. close ();
}
If (con! = Null ){
Con. close ();
}
}
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Class Msg {
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Integer id;
String url;
Public String getUrl (){
Return url;
}
Public void setUrl (String url ){
This. url = url;
}
}
}