XML文件转换成 HTML

19 篇文章 0 订阅
1 篇文章 0 订阅

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>
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值