PDA

View Full Version : java - decodificare xml presente nel db


AntaniRifasato
12-10-2007, 17:19
Salve a tutti,
aiutoooo :confused: aiutoooo,
ho memorizzato un file xml all'interno del Db con questo metodo:

private String getXML(String xmlUrl) throws Exception {
try {
URL url = new URL(xmlUrl);
BufferedReader inp = new BufferedReader(new InputStreamReader(url.openStream()));
String xml = "";
while (true) {
String s = inp.readLine();
if ( s == null )
break;
xml += s;
}
inp.close();
return URLEncoder.encode(xml,"UTF8");
}
catch (Exception e) {
throw new Exception("Problema di connessione alla url:" + xmlUrl);
}
}

e prelevo l'xml così memorizzato nel db con queste istruzioni:

String query_select = "SELECT data FROM xml_data WHERE id=?";
String brand = (request.getPathInfo().trim()).substring(1);

connectionPool = ConnectionPoolSingleton.getInstance();
conn = connectionPool.getConnection();
pstmt = conn.prepareStatement(query_select);
pstmt.setString(1, brand);
ResultSet rs1 = pstmt.executeQuery();
if (rs1.next()) {
xml = URLDecoder.decode(rs1.getString("data"),"UTF8");
.......


Ora tutto bene con il get rileggo il contenuto del db, ovvero l'xml che ho memorizzato con il post, l'unico problema è che non mi legge le lettere accentate, ad esempio ì diventa �.
Dove sbaglio come posso fare a rileggere correttamente il valore, in realtà ho verificato ed il problema è a monte ovvero quando memorizzo nel db ho già quei caratteri strani al posto delle lettere accentate, del resto se non faccio l'encode mi dà errore quando lo vado a leggere.
Aiutatemi, :boh: :boh: :boh: :boh: :boh: - vi prego...
Ciao e grazie

AntaniRifasato
12-10-2007, 17:52
ora mi inserisce correttamente le lettere accentate nel Db con questa piccola modifica:
private String getXML(String xmlUrl) throws Exception {
try {
Charset charset = Charset.forName("ISO-8859-1");
URL url = new URL(xmlUrl);
BufferedReader inp = new BufferedReader(new InputStreamReader(url.openStream(),charset));
String xml = "";
while (true) {
String s = inp.readLine();
if ( s == null )
break;
xml += s;
}
inp.close();
return xml;
}
catch (Exception e) {
throw new Exception("Problema di connessione alla url:" + xmlUrl);
}
}

però quando vado a prelevare e dal db il file sbrocca quando vede le lettere accentata ora al posto di ì mi dà ì