简体   繁体   中英

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

我设法通过将 UNIX 服务器中的 JVM 编码更改为 ISO-8859-1 来解决该问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM