欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XML文件转换成 HTML 博客分类: XML xslxmlhtmljava 

程序员文章站 2024-02-20 22:33:52
...
package com.itheima.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;

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

public class TextXMLToHTML {
	/**
	 * 将XML转换成HTML
	 * @throws Exception
	 */
	public static void translate() throws Exception{
		//创建XML的文件输入流
		FileInputStream fis=new FileInputStream("F:/123.xml");
		Source source=new StreamSource(fis);
		
		//创建XSL文件的输入流
		FileInputStream fis1=new FileInputStream("F:/123.xsl");
		Source template=new StreamSource(fis1);
		
		PrintStream stm=new PrintStream(new File("F:/123.html"));
		//讲转换后的结果输出到 stm 中即 F:\123.html
		Result result=new StreamResult(stm);
		//根据XSL文件创建准个转换对象
		Transformer transformer=TransformerFactory.newInstance().newTransformer(template);
		//处理xml进行交换
		transformer.transform(source, result); 
		//关闭文件流
		fis1.close();
		fis.close();
	}
	
	public static void main(String[] args){
		try {
			translate();
		} catch (Exception e) {
			System.out.println("XML转换成HTML失败:"+e.getMessage());
		}
	}
}



F:/123.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<employees>
	<employee-list>
		<employee>
			<id>001</id>
			<name>李达</name>
			<gender>男</gender>
			<address>北京海淀</address>
		</employee>
		<employee>
			<id>002</id>
			<name>赵超</name>
			<gender>男</gender>
			<address>上海黄浦</address>
		</employee>
		<employee>
			<id>003</id>
			<name>张云</name>
			<gender>女</gender>
			<address>山东济南</address>
		</employee>
	</employee-list>
</employees>


F:/123.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>员工信息</title>
</head>

<body>
	<table width="800px" border="1" bordercolor="#000000" style="border-collapse:collapse">
		<tr>
            <td align="center">编号</td>
            <td align="center">姓名</td>
            <td align="center">性别</td>
            <td align="center">地址</td>
        </tr>
        <xsl:for-each select="employees/employee-list/employee">
			<tr>
                <td align="center"><xsl:value-of select="id"/></td>
                <td align="center"><xsl:value-of select="name"/></td>
                <td align="center"><xsl:value-of select="gender"/></td>
                <td align="center"><xsl:value-of select="address"/></td>	
            </tr>
        </xsl:for-each>

    </table>
</body>
</html>

</xsl:template>
</xsl:stylesheet>


//运行后在F:/生成了123.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工信息</title>
</head>
<body>
<table width="800px" border="1" bordercolor="#000000" style="border-collapse:collapse">
<tr>
<td align="center">编号</td><td align="center">姓名</td><td align="center">性别</td><td align="center">地址</td>
</tr>
<tr>
<td align="center">001</td><td align="center">李达</td><td align="center">男</td><td align="center">北京海淀</td>
</tr>
<tr>
<td align="center">002</td><td align="center">赵超</td><td align="center">男</td><td align="center">上海黄浦</td>
</tr>
<tr>
<td align="center">003</td><td align="center">张云</td><td align="center">女</td><td align="center">山东济南</td>
</tr>
</table>
</body>
</html>