1. 准备工作
staff.dat——被解析的文件
张三|100.0|1998|1|1李四|200.0|1997|2|2
王五|300.0|1996|3|3
makehtml.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"></xsl:output>
<xsl:template match="/staff">
<table border="1"><xsl:apply-templates/></table>
</xsl:template>
<xsl:template match="/staff/employee">
<tr><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="/staff/employee/name">
<td><xsl:apply-templates/></td>
</xsl:template>
<xsl:template match="/staff/employee/salary">
<td>$<xsl:apply-templates/></td>
</xsl:template>
<xsl:template match="/staff/employee/hiredate">
<td><xsl:value-of select="@year"/>-<xsl:value-of select="@month"/>-<xsl:value-of select="@day"/></td>
</xsl:template>
</xsl:stylesheet>
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
public class Test5 {
public static void main(String[] args) {
try {
File file=new File("e:\\makehtml.xsl");//xsl文件
StreamSource stylesource=new StreamSource(file);//根据xsl文件获取streamsource对象
Transformer t=TransformerFactory.newInstance().newTransformer(stylesource);//设置了格式 如果不设置 默认为输出xml格式
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "html");//输出格式为html
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.setOutputProperty(OutputKeys.ENCODING, "gb2312");//支持中文
t.transform(new SAXSource(new EmployeeReader(), new InputSource(new FileInputStream(new File("e:\\staff.dat")))), new StreamResult(System.out));//控制台打印输出 如果要输出到本地 改变System.out为输出流即可
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*
*
* @description xml解析器 执行具体解析工作
* @author 赵伟
* @date 创建日期: 2013-1-7
* @date 参数:
*/
class EmployeeReader implements XMLReader{
private ContentHandler handler;
public ContentHandler getContentHandler() {
// TODO Auto-generated method stub
return handler;
}
public DTDHandler getDTDHandler() {
// TODO Auto-generated method stub
return null;
}
public EntityResolver getEntityResolver() {
// TODO Auto-generated method stub
return null;
}
public ErrorHandler getErrorHandler() {
// TODO Auto-generated method stub
return null;
}
public boolean getFeature(String name) throws SAXNotRecognizedException,
SAXNotSupportedException {
// TODO Auto-generated method stub
return false;
}
public Object getProperty(String name) throws SAXNotRecognizedException,
SAXNotSupportedException {
// TODO Auto-generated method stub
return null;
}
/**
* 解析文件为xslt能识别的格式 在这里解析的文件为staff.dat
*/
public void parse(InputSource input) throws IOException, SAXException {
// TODO Auto-generated method stub
InputStream stream=input.getByteStream();//转换为流
BufferedReader breader=new BufferedReader(new InputStreamReader(stream));//包装
String rootele="staff";//根节点
AttributesImpl atts=new AttributesImpl();
if (handler==null) {
return;
}
handler.startDocument();//开启文档
handler.startElement("", rootele, rootele, atts);//开启根节点
String line;
while ((line=breader.readLine())!=null) {//循环遍历行 在这个例子里共有三行
handler.startElement("", "employee", "employee", atts);
StringTokenizer t=new StringTokenizer(line,"|");
handler.startElement("", "name", "name", atts);
String s=t.nextToken();
handler.characters(s.toCharArray(), 0, s.length());
handler.endElement("", "name", "name");
handler.startElement("", "salary", "salary", atts);
String s2=t.nextToken();
handler.characters(s2.toCharArray(), 0, s2.length());
handler.endElement("", "salary", "salary");
//设置属性
atts.addAttribute("", "year", "year", "CDATA", t.nextToken());
atts.addAttribute("", "month", "month", "CDATA", t.nextToken());
atts.addAttribute("", "day", "day", "CDATA", t.nextToken());
handler.startElement("", "hiredate", "hiredate", atts);//将属性与日期关联
handler.endElement("", "hiredate", "hiredate");
atts.clear();//清空
handler.endElement("", "employee", "employee");//结束节点 上面同理
}
handler.endElement("", rootele, rootele);//结束根节点
handler.endDocument();//结束文档
}
public void parse(String systemId) throws IOException, SAXException {
// TODO Auto-generated method stub
}
public void setContentHandler(ContentHandler handler) {
// TODO Auto-generated method stub
this.handler=handler;
}
public void setDTDHandler(DTDHandler handler) {
// TODO Auto-generated method stub
}
public void setEntityResolver(EntityResolver resolver) {
// TODO Auto-generated method stub
}
public void setErrorHandler(ErrorHandler handler) {
// TODO Auto-generated method stub
}
public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException {
// TODO Auto-generated method stub
}
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
// TODO Auto-generated method stub
}
}
测试可用,这是书中的小例子,自己练习用~