title image


Smiley Re: XML-Datei aus MySQL-DB erzeugen.
Ich hab hier ne interessante Klasse für dich.

Nur die Klasse XMLFilter für das filtering habe

ich jetzt nicht beigelegt (also musst du das vor dem

Kompilieren auskommentieren).

Sag erstmal, ob das was für dich ist.





import java.sql.Connection;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;



/** this abstract class is used to transform an SQL-Query´s result

* to XML.

* @author Nic2

*/

public abstract class SQL2XML {



static final boolean filtering = true;



/** Creates a new instance of SQL2XML */

public SQL2XML() {

}

/**

*generates an XML-Structure containing all data the SQL-Query would select.

*a tree of three elements can be specified by the user.

*the names of the elements containing the data are the field names specified by the SQL-Query.

*

* All contents will be checked for XML-Chars like '' and so on with the abstract class

* XMLFilter. If not needed, set filtering to "false".

*

*@param String sqlstr - the Query to be used

*@param Connection con - the Connection object to be used

*@param String root - the name of the root element

*@param String group - the name of an optional element. If "", it will not be used.

*@param String entry - the name of the element containing a record

**/

public static String generate(String sqlstr, Connection con, String root, String group, String entry) throws Exception {



//ueberpruefen auf Vollstaendigkeit

if (root.equals("") == true){root = "root";}

if (entry.equals("") == true){entry = "entry";}



try {

Statement st = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,

ResultSet.CONCUR_UPDATABLE);

ResultSet rs = st.executeQuery(sqlstr);

ResultSetMetaData rsmd = rs.getMetaData();



StringBuffer sb = new StringBuffer();

sb.append("");

if (group.equals("") == false){sb.append("");}

int numberOfColumns = rsmd.getColumnCount();

String colName = new String();



//Entries

int i=0;

StringBuffer colValue;

while(rs.next() == true){

sb.append("");

//Columns

for (i=1;i<=numberOfColumns;i++){

colValue = new StringBuffer();

colName = rsmd.getColumnName(i);

colValue.append(rs.getString(i));

if (colValue.toString() == null) colValue.append(" ");

if (filtering == true) { XMLFilter.filterXMLChars(colValue);}

sb.append("" + colValue.toString() + "");

}

sb.append("");

}



if (group.equals("") == false){sb.append("");}

sb.append("");

return sb.toString();

} catch (SQLException s) {

throw s;

}

}





}


---------------------------------------------------------------------------
"The hero, then, is not time, but timelessness."
(Henry Miller)

geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: