`
lanqiaoyeyu
  • 浏览: 24337 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java 将XML和XSL转换成HTML

    博客分类:
  • Java
阅读更多
将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"/>
        <xsl:apply-templates select="./section"/>
    </xsl:template>
    <xsl:template match="chapter/section/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"/>
        <xsl:number value="123456789" grouping-separator="," grouping-size="3"/>
    </xsl:template>
</xsl:stylesheet>

分享到:
评论
1 楼 wuhenjia 2017-04-25  
楼主,你好,在使用上述方式转为html时发现部分丢失,且拍好的序被打乱,不知是何故?

相关推荐

Global site tag (gtag.js) - Google Analytics