0

I'd just written a java file using Eclipse encoding with ISO-8859-1. In this file, I want to create a String such like that (in order to create a XML content and save it into a database) :

//   <image>&lt;img src="path_of_picture"&gt;</image>
String xmlContent = "<image><img src=\"" + path_of_picture+ "\"></image>"; 

In another file, I get this String and create a new String with this constructor :

String myNewString = new String(xmlContent.getBytes(), "UTF-8");

In order to be understood by a XML parser, my XML content must be converted to :

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

Unfortunately, I can't find how to write xmlContent to get this result in myNewString. I tried two methods :

       // First : 
String xmlContent = "<image><img src=\"" + content + "\"></image>"; 
// But the result is just myNewString = <image><img src="path_of_picture"></image>
// and my XML parser can't get the content of <image/>

    //Second :
String xmlContent = "<image>&lt;img src=\"" + content + "\"&gt;</image>";
// But the result is just myNewString = <image>&amp;lt;img src="path_of_picture"&amp;gt;</image>

Do you have any idea ?

  • Which xml parser u are using? I do not think that '' symbols in any form inside the string would be parsed by any one of the parsers. – rahulserver Jul 18 '13 at 08:35
  • How is this related to text encodings? The & character is ASCII, so it's encoded the same in UTF-8 and iso-8859-1, no possibility for confusion there. – Joni Jul 18 '13 at 08:35
  • 1
    Your problem is more likely related to character escaping, rather than encoding. Do you know about [CDATA](http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean)? – reto Jul 18 '13 at 08:36
  • That's what I thought, but the value of the text is different from the original String when I save it into my database. – Gaël Varlet Jul 18 '13 at 08:37
  • I didn't know CDATA. I'll try with it – Gaël Varlet Jul 18 '13 at 08:40
  • That's a good way. It works ! – Gaël Varlet Jul 18 '13 at 11:57

2 Answers2

0

This is unclear. But Strings don't have an encoding. So when you write

String s = new String(someOtherString.getBytes(), someEncoding);

you will get various results depending on your default encoding setting (which is used for the getBytes() method).

If you want to read a file encoded with ISO-8859-1, you simply do:

  • read the bytes from the file: byte[] bytes = Files.readAllBytes(path);
  • create a string using the file's encoding: String content = new String(bytes, "ISO-8859-1);

If you need to write back the file with a UTF-8 encoding you do:

  • convert the string to bytes with UTF-8 encoding: byte[] utfBytes = content.getBytes("UTF-8");
  • write the bytes to the file: Files.write(path, utfBytes);
assylias
  • 297,541
  • 71
  • 621
  • 741
0

I dont feel that your question is related to encoding but if you want to "create a String such like that (in order to create a XML content and save it into a database)", you can use this code:

public static Document loadXMLFromString(String xml) throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        return builder.parse(is);
    }

Refer this SO answer.

Community
  • 1
  • 1
rahulserver
  • 7,839
  • 18
  • 75
  • 137