我写了一个方法,使用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>
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>
解决方案 »
- 用java冒泡排一句话
- 请问什么时候释放对象锁?
- JInternalFrame之间的切换(急需)
- 请问如何在TOMCAT里面设置文件的输出类型?
- 有谁知道上海晟峰软件怎么样
- 莱鸟提问
- 请问System.in.read()
- 请教,int i=0;i=i++;在c++中输出i=1而在java中输出i=0???
- 使用socket如何开发Server/Client程序?
- 如何在文本区中显示java文本文件?
- java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'sa' 登录失败。
- java mail 使用pop3的协议怎么判断新邮件
你保存时候,你选中文编码或UTF-8试下...
加上
<xsl:output method="html" encoding="gb2312" media-type="text/html"/>
差不多就好了
<?xml version="1.0" encoding="gb2312"?>
两者不一致,改成GBK试试
其次看下
http://blog.csdn.net/sunyujia/archive/2008/06/21/2572479.aspx