兄弟, 数据库中的中文直接是倒不到XML中的,会出现乱码。建议在倒出后先通过字符转化,统一到UNICODE,在写入XML文件。

解决方案 »

  1.   

    怎么进行字符化,统一到UNICODE中?能不能说的详细点,小弟也遇上这种问题了
      

  2.   

    你看这样行不行
    public class XMLEncode {  public XMLEncode() {
      }
      public static InputStream getStandardXML(String xmlstr) {
        if(xmlstr==null) return null;
        String tmp = "GBK";
          if(xmlstr.indexOf(tmp)!=-1) {
            int start = xmlstr.indexOf(tmp);
            int end = start + 3;
            xmlstr = xmlstr.substring(0,start)+"GB2312"+xmlstr.substring(end,xmlstr.length());
          }
          byte[] xmlbyte= xmlstr.getBytes();
          InputStream inStream = new ByteArrayInputStream(xmlbyte);
          return inStream;
      }
    }
      

  3.   

    你用的是dom4j吗?用dom4j默认生成的document 文档的头是
    <? xml versin="1.0" encoding="utf-8" ?>
    的,如果你需要输出这个xml的话,不管是以何种形式:Stream,String
    只要显示中文的,都要在输出的时候设置一下OutputFormat outFormat = OutputFormat.createPrettyPrint();
    outFormat.setEncoding("ISO-8859-1");
    XMLWriter out = new XMLWriter(bao, outFormat);注:我用的dom4j版本只支持GBK编码,没有GB2312