我写了一个方法,使用XML+XSLT生成HTML,XML文件中有中文内容,如果XML文件用记事本或EditPlus编辑生成的html文件可以正确的显示中文内容,如果使用Dreamweaver来编辑XML文件,中文内容就会变成乱码,请问有什么解决方法吗? 代码如下:
public class GenerateHtmlMethod{
public String getHtml(String xml,String xslt){
String str,data="";
    try{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new FileReader(xml)));
BufferedReader readerr = new BufferedReader(new FileReader(xml));             StreamSource xsltSource = new StreamSource(new FileInputStream(xslt));
        Source source = new DOMSource(document);
            TransformerFactory tf = TransformerFactory.newInstance();
            Templates transformation = tf.newTemplates(xsltSource);
        Transformer transformer = transformation.newTransformer();         File file=new File("s.html");
        FileOutputStream out=new FileOutputStream(file);
            StreamResult result=new StreamResult(out);
        transformer.transform(source, result);
            
            BufferedReader reader = new BufferedReader(new FileReader(file));          
            while(true){     
                str = reader.readLine();    
                if (str==null) break;    
                data += str;  
            }     
            reader.close();
    }catch(Exception e){
    System.out.println("Exception:"+e);
    }
  return data;
    }
public static void main(String args[]){
        GenerateHtmlMethod yang =  new GenerateHtmlMethod();
        String html=yang.getHtml("11-1.xml","1.xslt");
        System.out.println(html);
    }
}XML文件(11-1.xml):
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet Type="text/xsl" href="1.xslt"?>
<product>  
  <product_name>Avanda MP4 N181 乱码 DigitalPlayer </product_name> 
   <item>NO.A455-1028</item>
   <price>$110.98</price>
   <list_price>$120.98</list_price>
   <you_save>$10.00</you_save>
   <grade_rating>images/4-stars.gif</grade_rating>
</product>XSLT(1.xslt):<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="anything here"  >
<xsl:template match="/">
<html>
<head>
<style type="text/css">
body {TEXT-ALIGN: center;}
#center { MARGIN-RIGHT: auto; MARGIN-LEFT: auto; } 
.specials{ font-family:Arial, Helvetica, sans-serif; font-size:20px; font-weight:bold; text-align:left}
.apply{ font-family:Arial, Helvetica, sans-serif; font-size:13px; text-align:left}
</style>
</head>
<body topmargin="0">
<div  id="center" style="width:420px;">
 <div>
<table cellpadding="0" cellspacing="0" border="0"><tr ><td  valign="middle" height="40" bgcolor="#f3f3f3" width="420" style="font-family:Arial, Helvetica, sans-serif; font-size:20px;" align="center"><xsl:value-of select="product/product_name"></xsl:value-of></td></tr></table> 
 </div>
 <div style="font-family:Arial, Helvetica, sans-serif; font-size:14px; text-align:left; margin-left:65px; margin-top:7px; margin-bottom:18px; font-weight: 500;">Item: <xsl:value-of select="product/item"></xsl:value-of></div>
 <div style="margin-left:65px; font-family:Arial, Helvetica, sans-serif; font-size:13px; line-height:25px;">
 <div style="float:left">Our Price:</div><div style="float:left; margin-left:23px;  color:#d90000; font-weight:bold; letter-spacing:1.2px;"><xsl:value-of select="product/price"></xsl:value-of></div><br/>
 <div style="float:left">List Price:</div><div style="float:left; margin-left:26px; font-weight:bold; color:#222222; letter-spacing:1.2px;"><xsl:value-of select="product/list_price"></xsl:value-of></div><br/>
 <div style="float:left">Your Save:</div><div style="float:left; margin-left:23px; font-weight:bold; color:#222222; letter-spacing:1.2px;"><xsl:value-of select="product/you_save"></xsl:value-of></div><br/>
 <div style="float:left; margin-top:15px;">Quantity:</div>
 <div style="float:left; margin-left:30px; margin-top:15px;">
 <select>
   <option value="first" selected="selected">1</option>
   <option>2</option>
   <option>3</option>
   <option>4</option>
 </select>
 </div>
 <div style="clear:both"> 
 <div style="float:left; margin-top:5px;">Grade:</div><div style="float:left; margin-left:23px; margin-top:10px;">
 <xsl:variable   name="pic"><xsl:value-of   select="product/grade_rating"  /></xsl:variable>   
                   <img src="{$pic}"   />       
 </div></div>
 <div style="clear:both; text-align:left"><img src="images/Unigroup-web-design-product.2.7_16.gif"/></div>
 </div>
 
 </div>
</body>
</html></xsl:template>
</xsl:stylesheet>

解决方案 »

  1.   

    好象是Dreamweaver不支持中文,用其他工具吧
      

  2.   

    可能与你存储xml编码有关系吧,
    你保存时候,你选中文编码或UTF-8试下...
      

  3.   

    在你的XSLT(1.xslt)中<xsl:template match="/">的前面
    加上
    <xsl:output method="html" encoding="gb2312" media-type="text/html"/>
    差不多就好了
      

  4.   

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml version="1.0" encoding="gb2312"?> 
    两者不一致,改成GBK试试
      

  5.   

    的确与xml编码格式有关,我用winhex看了下,用记事本编辑的xml和用Deamweaver编辑的xml不一样,两个汉字用Deamweaver编辑的xml中会占用6个字节,不知道是怎么回事,有谁知道deamweaver采用的是什么编码格式吗?
      

  6.   

    Dreamweaver中文乱码问题lz 找到 页面属性----------->标题/编码----------->编码改成简体中文(GB2312);改一下
      

  7.   

    首先顶koko的
    其次看下
    http://blog.csdn.net/sunyujia/archive/2008/06/21/2572479.aspx
      

  8.   

    谢谢大家! 特别感谢kokobox和sunyujia 问题解决了 
      

  9.   

    感谢tanlingyun,你的答案是对的。