Ciao a tutti,
ecco il mio problema:
ho un file di input (.txt con codifica utf8) uso un InputStreamReader per leggerlo, specificandone la codifica 8tf8. E fin qui tutto ok. Ora devo aggiungere a questo file dei campi (devo renderlo un xml) quindi gli aggiungo (se necessario) l'header xml e il nodo radice che apre e chiude a fine testo.
Una volta aggiunti questi valori mi ritrovo con un valore "carattere a capo" e "?" tra l'intestazione che inserisco e il resto del file, precisamente dopo <ROOT> mi esce "carattere a capo" e "?".
Non so da cosa dipenda, forse le stringhe che aggiungo devono essere utf8 anche loro? se, si come posso ovviare? Grazie a tutti
codice:
public String generateXmlString () {
String xmlString = new String();
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8") );
String text = null;
boolean header = false;
boolean end = false;
// repeat until all lines is read
while ((text = reader.readLine()) != null){
if(text.contains("<?xml") && (!header)) {
contents.append(text).append(System.getProperty("line.separator"));
header = true;
}
else if (!header){
contents.append("<?xml version=\"1.0\"?>").append(System.getProperty("line.separator"));
contents.append("<ROOT>").append(System.getProperty("line.separator"));
contents.append(text).append(System.getProperty("line.separator"));
header = true;
end = true;
}
else {
contents.append(text).append(System.getProperty("line.separator"));
}
}
if(end){
contents.append("</ROOT>");
}
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
finally{
try {
if (reader != null){
reader.close();
}
} catch (IOException e){
e.printStackTrace();
}
}
xmlString = new String (contents.toString());
return xmlString;
}