您的位置:首页 > Web前端 > HTML

xml+xsl生成html的方法

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



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);

	}

}

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

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

<syj>

	<test>测试</test>

</syj>

<?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文件的情况,修改代码如下。

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();

	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: