Glen
Glen

Reputation: 11

Java XML Encoding for GB2312 with ISO-8859-1 on the declaration

I am working to migrate my code that creates XML from Java 1.4 to 1.8. From the old java code, it encodes the xml file into GB2312 and the XML declaraction is this <?xml version="1.0" encoding="ISO-8859-1"?> with correct chinese characters. But when I moved to java 1.8, the entire XML turn into ISO-8859-1 and the chinese character turn into something like this &#x5a01;&#x731b;

      String values = "急需";
      byte[] encoded = values.getBytes("GB2312");
      Text env = doc.createTextNode(new String(encoded,"GB2312"));
      envelope.appendChild(env);
                
      doc.appendChild(messages);
      OutputFormat format  = new OutputFormat(doc,"ISO-8859-1", true);

      String path = "C:\\Test\\tes112.xml";
      fs = new FileOutputStream(path);
      
      
      XMLSerializer serial = new XMLSerializer(fs,format);          
      serial.serialize(doc);

Here is the sample output from the old code XML File with ISO-8859-1 declaration and GB2312 Encoding

Here is the sample output from the new code

XML File with ISO-8859-declaration and with encoding issue on GB2312

Thanks for your help

Upvotes: 1

Views: 299

Answers (1)

Glen
Glen

Reputation: 11

I manage to fix the issue by changing the JVM Encoding in UNIX server to ISO-8859-1

Upvotes: 0

Related Questions