xml+xslt文件生成html的汉字乱码问题
我写了一个方法,使用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>