xml+xsl生成html的方法

今天在论坛上看到一位朋友在利用xml+xsl生成html的时候,出现了乱码,在他代码的基础上做了下修改,解决了乱码问题。不知道是不是利用这种方法只能生成UTF-8的格式。

 

view plaincopy to clipboardprint?
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;  
 
 
 
/** 
 
 * <p> 
 
 * Title:将xml+xsl生成html 
 
 * </p> 
 
 *  
 
 * <p> 
 
 * Copyright: 转载请注明出处http://blog.csdn.net/sunyujia/ 
 
 * </p> 
 
 *  
 
 * @author 孙钰佳 
 
 * @main sunyujia@yahoo.cn 
 
 * @date Jun 21, 2008 12:40:38 PM 
 
 */ 
 
public class Test {  
 
    public static String buildHtml(String xml, String xsl) throws Exception {  
 
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory  
 
                .newInstance();  
 
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();  
 
        Document xmlDoc = builder  
 
                .parse(new ByteArrayInputStream(xml.getBytes()));  
 
        Source xmlSource = new DOMSource(xmlDoc);  
 
        StreamSource xslSource = new StreamSource(new ByteArrayInputStream(xsl  
 
                .getBytes()));  
 
        TransformerFactory tf = TransformerFactory.newInstance();  
 
        Templates transformation = tf.newTemplates(xslSource);  
 
        Transformer transformer = transformation.newTransformer();  
 
 
 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();  
 
        StreamResult result = new StreamResult(bos);  
 
        transformer.transform(xmlSource, result);  
 
        return new String(bos.toByteArray(), "UTF-8");// 指定UTF-8解决乱码问题  
                     //如果这里不指定UTF-8的话需指定xsl为GBK方式输出  
                  //<xsl:output method="html" encoding="GBK" indent="yes"/>   
                  //<xsl:template match="/">  
    }  
 
 
 
    public static String readFileToString(File file) throws IOException {  
 
        StringBuffer sb = null;  
 
        BufferedReader in = null;  
 
        try {  
 
            in = new BufferedReader(new FileReader(file));  
 
            sb = new StringBuffer();  
 
            for (String line; (line = in.readLine()) != null;) {  
 
                sb.append(line + "\r\n");  
 
            }  
 
        } finally {  
 
            if (in != null)  
 
                in.close();  
 
        }  
 
        return sb.toString();  
 
    }  
 
 
 
    public static void main(String[] args) throws Exception {  
 
        String xmlStr = readFileToString(new File(Test.class.getResource(  
 
                "test.xml").getFile()));  
 
        String xslStr = readFileToString(new File(Test.class.getResource(  
 
                "test.xsl").getFile()));  
 
        String html = buildHtml(xmlStr, xslStr);  
 
        System.out.println(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;

 

/**

 * <p>

 * Title:将xml+xsl生成html

 * </p>

 *

 * <p>

 * Copyright: 转载请注明出处http://blog.csdn.net/sunyujia/

 * </p>

 *

 * @author 孙钰佳

 * @main sunyujia@yahoo.cn

 * @date Jun 21, 2008 12:40:38 PM

 */

public class Test {

 public static String buildHtml(String xml, String xsl) throws Exception {

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory

    .newInstance();

  DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();

  Document xmlDoc = builder

    .parse(new ByteArrayInputStream(xml.getBytes()));

  Source xmlSource = new DOMSource(xmlDoc);

  StreamSource xslSource = new StreamSource(new ByteArrayInputStream(xsl

    .getBytes()));

  TransformerFactory tf = TransformerFactory.newInstance();

  Templates transformation = tf.newTemplates(xslSource);

  Transformer transformer = transformation.newTransformer();

 

  ByteArrayOutputStream bos = new ByteArrayOutputStream();

  StreamResult result = new StreamResult(bos);

  transformer.transform(xmlSource, result);

  return new String(bos.toByteArray(), "UTF-8");// 指定UTF-8解决乱码问题
                     //如果这里不指定UTF-8的话需指定xsl为GBK方式输出
                  //<xsl:output method="html" encoding="GBK" indent="yes"/>
                  //<xsl:template match="/">
 }

 

 public static String readFileToString(File file) throws IOException {

  StringBuffer sb = null;

  BufferedReader in = null;

  try {

   in = new BufferedReader(new FileReader(file));

   sb = new StringBuffer();

   for (String line; (line = in.readLine()) != null;) {

    sb.append(line + "\r\n");

   }

  } finally {

   if (in != null)

    in.close();

  }

  return sb.toString();

 }

 

 public static void main(String[] args) throws Exception {

  String xmlStr = readFileToString(new File(Test.class.getResource(

    "test.xml").getFile()));

  String xslStr = readFileToString(new File(Test.class.getResource(

    "test.xsl").getFile()));

  String html = buildHtml(xmlStr, xslStr);

  System.out.println(html);

 }

 

}
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="GBK"?> 
 
<?xml-stylesheet Type="text/xsl" href="test.xsl"?>   
 
<syj> 
 
    <test>测试</test> 
 
</syj> 

<?xml version="1.0" encoding="GBK"?>

<?xml-stylesheet Type="text/xsl" href="test.xsl"?>

<syj>

 <test>测试</test>

</syj>
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="GBK"?> 
 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
 
<xsl:template match="/"> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 
<xsl:value-of select="syj/test"/> 
 
</body> 
 
</html> 
 
</xsl:template> 
 
</xsl:stylesheet> 

<?xml version="1.0" encoding="GBK"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">

<html>

<head>

</head>

<body>

<xsl:value-of select="syj/test"/>

</body>

</html>

</xsl:template>

</xsl:stylesheet>  20080713追加

后来发现有include文件的情况,修改代码如下。

view plaincopy to clipboardprint?
public static String buildHtml(String xml, String xsl, final String basePath)  
 
        throws Exception {  
 
 
 
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory  
 
 
 
    .newInstance();  
 
 
 
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();  
 
 
 
    Document xmlDoc = builder.parse(new InputSource(new StringReader(xml)));  
 
 
 
    Source xmlSource = new DOMSource(xmlDoc);  
 
 
 
    StreamSource xslSource = new StreamSource(new StringReader(xsl));  
 
 
 
    TransformerFactory tf = TransformerFactory.newInstance();  
 
    URIResolver uriReslover = new URIResolver() {  
 
        public Source resolve(String href, String base)  
 
                throws TransformerException {  
 
            try {  
 
                //当遇到include文件的时候会执行此方法  
 
                // return new StreamSource(new StringReader(  
 
                // readFileToString(new URL(fileName))));  
 
                return null;  
 
            } catch (IOException e) {  
 
                e.printStackTrace();  
 
            }  
 
            return null;  
 
        }  
 
    };  
 
    tf.setURIResolver(uriReslover);  
 
    Templates transformation = tf.newTemplates(xslSource);  
 
 
 
    Transformer transformer = transformation.newTransformer();  
 
 
 
    StringWriter sw = new StringWriter();  
 
    StreamResult result = new StreamResult(sw);  
 
 
 
    transformer.transform(xmlSource, result);  
 
 
 
    return sw.toString();  
 
}
原文网址:http://blog.csdn.net/sunyujia/archive/2008/06/21/2572479.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值