xml转html例子2

xml转为html

2010-05-0413:29

依据一定的格式把xml转为html,要用到xsl。xsl语法可以在网上找详细资料。现贴出代码:

java类(我是通过一个http去访问的)

packageweb;

importjava.io.BufferedReader;

importjava.io.ByteArrayOutputStream;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.io.StringReader;

importjava.util.Properties;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjavax.xml.transform.OutputKeys;

importjavax.xml.transform.Transformer;

importjavax.xml.transform.TransformerException;

importjavax.xml.transform.TransformerFactory;

publicclassXmlToHtmlActionextendsHttpServlet{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

StringxmlContent=this.getFileContent("F:/szworkspace/play/WebRoot/xsl/cd_catalog.xml");

StringxslContent=this.getFileContent("F:/szworkspace/play/WebRoot/xsl/cd_catalog.xsl");

/*xslContent="<?xmlversion=\"1.0\"?>";

xslContent+="<xsl:stylesheetxmlns:xsl=\"http://www.w3.org/TR/WD-xsl\">";

xslContent+="</xsl:stylesheet>";*/

try{

Stringhtml=this.xmlToHtml(xslContent,xmlContent);

response.getWriter().print(html);

}catch(Exceptione){

e.printStackTrace();

}

}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doPost(request,response);

}

/**

*@desc获得文件内容,可能会有编码问题

*@paramfileName文件路径

**/

privateStringgetFileContent(StringfileName){

StringBuilderstrContent=newStringBuilder();

BufferedReaderbis=null;

try{

Filefile=newFile(fileName);

bis=newBufferedReader(newInputStreamReader(newFileInputStream(

file),"GBK"));

Stringline=null;

while((line=bis.readLine())!=null){

strContent.append(line).append("\n");

}

}catch(Exceptione){

e.printStackTrace();

}finally{

if(bis!=null){

try{

bis.close();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

returnstrContent.toString();

}

/**

*@desc将xml字符串转换为html

*@parambosXslSouxsl内容

*@parambosXmlSouxml内容

**/

privateStringxmlToHtml(StringxslContent,StringxmlContent)throwsTransformerException{

StringReaderbosXslSou=newStringReader(xslContent);

StringReaderbosXmlSou=newStringReader(xmlContent);

TransformerFactorytFactory=TransformerFactory.newInstance();

//获取xsl的字符流

Transformertransformer=tFactory.newTransformer(newjavax.xml.transform.stream.StreamSource(bosXslSou));

Propertiesproperties=transformer.getOutputProperties();

properties.setProperty(OutputKeys.ENCODING,"GBK");

properties.setProperty(OutputKeys.METHOD,"html");

transformer.setOutputProperties(properties);

ByteArrayOutputStreambosRes=newByteArrayOutputStream();

transformer.transform(newjavax.xml.transform.stream.StreamSource(bosXmlSou),newjavax.xml.transform.stream.StreamResult(bosRes));

StringsHtmlCont=newString(bosRes.toString());

returnsHtmlCont;

}

}

xsl文件:

<?xmlversion="1.0"encoding="GBK"?>

<xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0">

<xsl:outputmethod="html"encoding="GBK"/>

<xsl:templatematch="/">

<html>

<body>

<tableborder="2"bgcolor="yellow">

<tr>

<th>Title</th>

<th>Artist</th>

</tr>

<xsl:for-eachselect="CATALOG/CD">

<tr>

<td>

<xsl:value-ofselect="TITLE"/>

</td>

<td>

<xsl:value-ofselect="ARTIST"/>

</td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

xml文件:

<?xmlversion="1.0"encoding="GBK"?>

<CATALOG>

<CD>

<TITLE>EmpireBurlesque</TITLE>

<ARTIST>BobDylan</ARTIST>

<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

<CD>

<TITLE>测试</TITLE>

<ARTIST>CC</ARTIST>

<COUNTRY>USA</COUNTRY>

<COMPANY>Columbia</COMPANY>

<PRICE>10.90</PRICE>

<YEAR>1985</YEAR>

</CD>

</CATALOG>

然后自己配置sevelt吧。

相关推荐