java - How to keep character "&" from ISO-8859-1 to UTF-8 -


i'd written java file using eclipse encoding iso-8859-1. in file, want create string such (in order create xml content , save database) :

//   <image>&lt;img src="path_of_picture"&gt;</image> string xmlcontent = "<image><img src=\"" + path_of_picture+ "\"></image>";  

in file, string , create new string constructor :

string mynewstring = new string(xmlcontent.getbytes(), "utf-8"); 

in order understood xml parser, xml content must converted :

<image>&lt;img src="path_of_picture"&gt;</image> 

unfortunately, can't find how write xmlcontent result in mynewstring. tried 2 methods :

       // first :  string xmlcontent = "<image><img src=\"" + content + "\"></image>";  // result mynewstring = <image><img src="path_of_picture"></image> // , xml parser can't content of <image/>      //second : string xmlcontent = "<image>&lt;img src=\"" + content + "\"&gt;</image>"; // result mynewstring = <image>&amp;lt;img src="path_of_picture"&amp;gt;</image> 

do have idea ?

this unclear. strings don't have encoding. when write

string s = new string(someotherstring.getbytes(), someencoding); 

you various results depending on default encoding setting (which used getbytes() method).

if want read file encoded iso-8859-1, do:

  • read bytes file: byte[] bytes = files.readallbytes(path);
  • create string using file's encoding: string content = new string(bytes, "iso-8859-1);

if need write file utf-8 encoding do:

  • convert string bytes utf-8 encoding: byte[] utfbytes = content.getbytes("utf-8");
  • write bytes file: files.write(path, utfbytes);

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -