日期:2014-05-17  浏览次数:20616 次

Java 将XML和XSL转换成HTML
将test.xml和test.xsl文件转成test.html
package bill.com;

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XML2HTML {

	/**
	 * @param args
	 */
	public static void Transform(String xmlFileName, String xslFileName,
            String htmlFileName) {
        try {
            TransformerFactory tFac = TransformerFactory.newInstance();
            Source xslSource = new StreamSource(xslFileName);
            Transformer t = tFac.newTransformer(xslSource);
            File xmlFile = new File(xmlFileName);
            File htmlFile = new File(htmlFileName);
            Source source = new StreamSource(xmlFile);
            Result result = new StreamResult(htmlFile);
            System.out.println(result.toString());
            t.transform(source, result);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String xmlFileName = "C:\\Bill\\temp\\convert2HTML\\test.xml";
        String xslFileName = "C:\\Bill\\temp\\convert2HTML\\xsl.xsl";
        String htmlFileName = "C:\\Bill\\temp\\convert2HTML\\html.html";
        Transform(xmlFileName, xslFileName, htmlFileName);

	}

}



test.xml

<?xml version="1.0" encoding="utf-8"?>
<!--<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>-->
<book>
    <title>XML and JSP</title>
    <chapter>
        <title>XMLDTD</title>
        <section>
            <title>XML</title>
            <example>HelloWorld.html</example>
        </section>
    </chapter>
    <chapter>
        <title>2 XML</title>
        <section>
            <title>ada</title>
            <section>
                <title>adas</title>
                <example>people.xml</example>
            </section>
            <section>
                <title>ad</title>
                <example>book.xml</example>
            </section>
            <section>
                <title>adsas</title>
                <example>book2.xml</example>
            </section>
        </section>
        <section>
            <title>asddsd</title>
        </section>
    </chapter>
</book>



test.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <head>
                <title>Use XML+XSL change to HTML file</title>
            </head>
            <body>
                <xsl:apply-templates select="book"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="chapter">
        <br/>
        <br/>
        <xsl:value-of select="./title"/>
        <xsl:apply-templates select="./section"/>
    </xsl:template>
    <xsl:template match="chapter/section">
        <br/>
        <br/>
        <xsl:text>    </xsl:text>
        <!--<xsl:number format="1. " level="multiple"/>-->
        <xsl:number format="1. " level="multiple" count="chapter | section" from="book"/>
        <xsl:value-of select="./title"/&g