« SEO最新发现:HTML生成PDF推广方案WordPress优化指南 »

HTML解决乱码问题

发布:zyseo | 分类:程序与优化 | 评论:0 | 引用:0 | 浏览:

HTML解决乱码问题其中可以会产生乱码,解决方案如下:
import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Source; 
import javax.xml.transform.Templates; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; 
import org.w3c.dom.Document; 
public  Test { 
public  String buildHtml(String xml, String xsl) throws Exception { 
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory 
.Instance; 
DocumentBuilder builder = docBuilderFactory.DocumentBuilder; 
Document xmlDoc = builder 
.parse( ByteArrayInputStream(xml.getBytes)); 
Source xmlSource =  DOMSource(xmlDoc); 
StreamSource xslSource =  StreamSource( ByteArrayInputStream(xsl 
.getBytes)); 
TransformerFactory tf = TransformerFactory.Instance; 
Templates transformation = tf.Templates(xslSource); 
Transformer transformer = transformation.Transformer; 
ByteArrayOutputStream bos =  ByteArrayOutputStream; 
StreamResult result =  StreamResult(bos); 
transformer.transform(xmlSource, result); 
String(bos.toByteArray, "UTF-8");// 指定UTF-8解决乱码问题 

public  String readFile(File file) throws IOException { 
StringBuffer sb = null; 
BufferedReader in = null; 
try { 
in =  BufferedReader( FileReader(file)); 
sb =  StringBuffer; 
for (String line; (line = in.readLine) != null;) { 
sb.append(line + "\r\n"); 

} finally { 
(in != null) 
in.close; 

sb.toString; 

public  void (String args) throws Exception { 
String xmlStr = readFile( File(Test..getResource( 
"test.xml").getFile)); 
String xslStr = readFile( File(Test..getResource( 
"test.xsl").getFile)); 
String html = buildHtml(xmlStr, xslStr); 
.out.prln(html); 


import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.transform.Source; 
import javax.xml.transform.Templates; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import javax.xml.transform.stream.StreamSource; 
import org.w3c.dom.Document; 
 

tagTags:乱码解决  

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。