直接给代码:
package com.adam.xedit.service.server.xml; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.io.DocumentResult; import org.dom4j.io.DocumentSource; import org.dom4j.io.HTMLWriter; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; /** * 通过xsl文件转换相应的xml数据文件为html格式字符串 * @author hsy * */ public class Xml2HtmlString { private Logger log = Logger.getLogger(this.getClass().getName()); public Xml2HtmlString() { super(); } /** * 将xml以xsl样式转化为html字符串 * @param xmlString xml字符串 * @param xslPath xsl路径 * @return */ public String getHtmlString(String xmlString,String xslPath){ log.info("开始执行getHtmlString(...)方法"); String returnDocStr = ""; try { SAXReader reader = new SAXReader(); ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes()); Document doc = reader.read(bais); Document transformDoc = this.transformDocument(doc,xslPath); returnDocStr = this.write2String(transformDoc); log.info("getHtmlString(...)执行成功!"); } catch (Exception e) { log.info("getHtmlString(...)方法执行失败,提示信息["+e.getMessage()+"]"); } return returnDocStr; } /** * 通过xsl将xml数据文件转化doc对象 * @param doc xml文档对象 * @param xslPath xls文件路径 * @return */ private Document transformDocument(Document doc,String xslPath){ log.info("开始执行 transformDocument(...)方法"); TransformerFactory factory = TransformerFactory.newInstance(); Document transformerDoc = null; try { Transformer transformer = factory.newTransformer(new StreamSource(xslPath)); DocumentSource docSource = new DocumentSource(doc); DocumentResult docResult = new DocumentResult(); transformer.transform(docSource, docResult); transformerDoc = docResult.getDocument(); log.info("transformDocument(...)执行成功!"); } catch (Exception e) { log.info("transformDocument(...)方法执行失败,提示信息["+e.getMessage()+"]"); } return transformerDoc; } /** * 将doc文档对象转化为html字符串 * @param transformDoc doc文档 * @return */ private String write2String(Document transformDoc){ log.info("开始执行 write2String(...)方法"); StringWriter strWriter = new StringWriter(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK"); format.setXHTML(true); HTMLWriter htmlWriter = new HTMLWriter(strWriter,format); format.setExpandEmptyElements(false); try { htmlWriter.write(transformDoc); htmlWriter.flush(); log.info("write2String(...)执行成功!"); } catch (IOException e) { log.info("write2String(...)方法执行失败,提示信息["+e.getMessage()+"]"); } return strWriter.toString(); } public static void main(String[] args){ Xml2HtmlString obj = new Xml2HtmlString(); String xmlString = "<?xml version=\"1.0\" encoding=\"GBK\"?>" + "<?xml-stylesheet type=\"text/xsl\" href=\"xedit/xsl/CUSTOMER_Query.xsl\"?>" + "<rows tableName=\"HSY_T_CUSTOMER\" token=\"80C3ED58-28DB-264F-5E3C-0CB174373E6C\" pageType=\"query-page\" pageNo=\"1\" pageSize=\"20\" recordsCount=\"9\" pagesCount=\"1\" title=\"大客户查询\" workflow=\"false\" xsl=\"xedit/xsl/CUSTOMER_Query.xsl\" print-template=\"xedit/xsl/CUSTOMER_Query.xsl\" >" + "<queryRow>" + "<START_DATE align=\"right\" operator=\"ge\" fieldTitle=\"创建时间\" dataType=\"dateTime\" >2013-02-19 15:44:50.0</START_DATE>" + "<STOP_DATE align=\"right\" operator=\"le\" fieldTitle=\"停用时间\" dataType=\"dateTime\" >2030-02-19 15:44:52.0</STOP_DATE>" + "<CUSTOMER_NAME align=\"right\" operator=\"eq\" fieldTitle=\"大用户名称\" dataType=\"string\" >时代复分</CUSTOMER_NAME>" + "</queryRow>" + "<row>" + "<CUSTOMER_ID align=\"right\" fieldTitle=\"大用户ID\" dataType=\"NUMBER\" pk=\"true\" hidden=\"false\">2</CUSTOMER_ID>" + "<CUSTOMER_NAME align=\"left\" fieldTitle=\"大用户名称\" dataType=\"VARCHAR2\" pk=\"false\" hidden=\"false\">时代复分</CUSTOMER_NAME>" + "<START_DATE align=\"center\" fieldTitle=\"创建时间\" dataType=\"DATE\" pk=\"false\" hidden=\"false\">2013-02-19 15:44:50.0</START_DATE>" + "<STOP_DATE align=\"center\" fieldTitle=\"停用时间\" dataType=\"DATE\" pk=\"false\" hidden=\"false\">2030-02-19 15:44:52.0</STOP_DATE>" + "<X align=\"left\" fieldTitle=\"X坐标\" dataType=\"VARCHAR2\" pk=\"false\" hidden=\"false\">456312</X>" + "<Y align=\"left\" fieldTitle=\"Y坐标\" dataType=\"VARCHAR2\" pk=\"false\" hidden=\"false\">456789</Y>" + "</row>" + "</rows>"; String projectPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();// /X:/dev_env/server/tomcat-5.0.28/webapps/adam.workflow.frame/WEB-INF/classes/ projectPath = projectPath.substring(0, projectPath.lastIndexOf("/")); projectPath = projectPath.substring(0, projectPath.lastIndexOf("/")); projectPath = projectPath.substring(0, projectPath.lastIndexOf("/") + 1);// WebRoot根路径 String xlsPath = projectPath+"xedit/xsl/CUSTOMER_Query.xsl"; String str = obj.getHtmlString(xmlString, xlsPath); System.out.println("str=="+str); } }
str== <html xmlns:fo="http://www.w3.org/1999/XSL/Format"> <head> <link href="css/public.css" type="text/css" rel="stylesheet" /> <script src="js/My97DatePicker/WdatePicker.js" type="text/javascript"/> </head> <body> <table id="xlsQueryTable" width="100%" style="width:800;"> <tr> <td align="right">创建时间[ 大于等于]</td> <td> <input align="right" type="text" id="START_DATE" name="START_DATE" value="" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" style="operator:ge;dataType:dateTime;" /> </td> <td align="right">停用时间[ 小于等于]</td> <td> <input align="right" type="text" id="STOP_DATE" name="STOP_DATE" value="" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate" style="operator:le;dataType:dateTime;" /> </td> </tr> <tr> <td align="right">大用户名称</td> <td> <input align="right" type="text" id="CUSTOMER_NAME" name="CUSTOMER_NAME" value="" style="operator:eq;dataType:string;" /> </td> </tr> </table> </body> </html>
CUSTOMER_Query.xsl文件:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <html> <head> <link rel="stylesheet" type="text/css" href="css/public.css"></link> <script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script> </head> <body> <table style="width:800;" width="100%" id="xlsQueryTable"> <!-- 每一个字段显示两列:字段名称、字段值输入框; 1.生成字段名称时: 对于xml中的operator属于 > 、 >= 、< 、<= 等要在页面显示相应的文字; 2.生成字段输入框时:对于xml中的属性也要生成页面元素,包括[align、type、id、name]; 判断数据类型以确定其在页面中显示的输入库的样式; --> <tr> <!-- 字段 START_DATE --> <td> <xsl:attribute name="align"><xsl:value-of select="rows/queryRow/START_DATE/attribute::align"/></xsl:attribute> <xsl:value-of select="rows/queryRow/START_DATE/attribute::fieldTitle"></xsl:value-of> <xsl:choose> <xsl:when test="rows/queryRow/START_DATE/@operator='ge'">[ 大于等于]</xsl:when> <xsl:when test="rows/queryRow/START_DATE/@operator='gt'">[ 大于]</xsl:when> <xsl:when test="rows/queryRow/START_DATE/@operator='le'">[ 小于等于]</xsl:when> <xsl:when test="rows/queryRow/START_DATE/@operator='lt'">[ 小于]</xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> </td> <td> <xsl:element name="input"> <xsl:attribute name="align"> <xsl:value-of select="rows/queryRow/START_DATE/attribute::align"></xsl:value-of> </xsl:attribute> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="id">START_DATE</xsl:attribute> <xsl:attribute name="name">START_DATE</xsl:attribute> <xsl:attribute name="value"></xsl:attribute> <!-- 判断输入框格式 --> <xsl:choose> <xsl:when test="rows/queryRow/START_DATE/@dataType='date'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/START_DATE/@dataType='dateTime'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/START_DATE/@dataType='time'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> <xsl:attribute name="style">operator:<xsl:value-of select="rows/queryRow/START_DATE/attribute::operator"></xsl:value-of>;dataType:<xsl:value-of select="rows/queryRow/START_DATE/attribute::dataType"></xsl:value-of>;</xsl:attribute> </xsl:element> </td> <!-- 字段 STOP_DATE --> <td> <xsl:attribute name="align"><xsl:value-of select="rows/queryRow/STOP_DATE/attribute::align"/></xsl:attribute> <xsl:value-of select="rows/queryRow/STOP_DATE/attribute::fieldTitle"></xsl:value-of> <xsl:choose> <xsl:when test="rows/queryRow/STOP_DATE/@operator='ge'">[ 大于等于]</xsl:when> <xsl:when test="rows/queryRow/STOP_DATE/@operator='gh'">[ 大于]</xsl:when> <xsl:when test="rows/queryRow/STOP_DATE/@operator='le'">[ 小于等于]</xsl:when> <xsl:when test="rows/queryRow/STOP_DATE/@operator='lh'">[ 小于]</xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> </td> <td> <xsl:element name="input"> <xsl:attribute name="align"> <xsl:value-of select="rows/queryRow/STOP_DATE/attribute::align"></xsl:value-of> </xsl:attribute> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="id">STOP_DATE</xsl:attribute> <xsl:attribute name="name">STOP_DATE</xsl:attribute> <xsl:attribute name="value"></xsl:attribute> <!-- 判断输入框格式 --> <xsl:choose> <xsl:when test="rows/queryRow/STOP_DATE/@dataType='date'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/STOP_DATE/@dataType='dateTime'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/STOP_DATE/@dataType='time'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> <xsl:attribute name="style">operator:<xsl:value-of select="rows/queryRow/STOP_DATE/attribute::operator"></xsl:value-of>;dataType:<xsl:value-of select="rows/queryRow/STOP_DATE/attribute::dataType"></xsl:value-of>;</xsl:attribute> </xsl:element> </td> </tr> <!-- 字段 CUSTOMER_NAME --> <tr> <td> <xsl:attribute name="align"><xsl:value-of select="rows/queryRow/CUSTOMER_NAME/attribute::align"/></xsl:attribute> <xsl:value-of select="rows/queryRow/CUSTOMER_NAME/attribute::fieldTitle"></xsl:value-of> <xsl:choose> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@operator='ge'">[ 大于等于]</xsl:when> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@operator='gh'">[ 大于]</xsl:when> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@operator='le'">[ 小于等于]</xsl:when> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@operator='lh'">[ 小于]</xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> </td> <td> <xsl:element name="input"> <xsl:attribute name="align"> <xsl:value-of select="rows/queryRow/CUSTOMER_NAME/attribute::align"></xsl:value-of> </xsl:attribute> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="id">CUSTOMER_NAME</xsl:attribute> <xsl:attribute name="name">CUSTOMER_NAME</xsl:attribute> <xsl:attribute name="value"></xsl:attribute> <!-- 判断输入框格式 --> <xsl:choose> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@dataType='date'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@dataType='dateTime'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:when test="rows/queryRow/CUSTOMER_NAME/@dataType='time'"> <xsl:attribute name="onfocus">WdatePicker({dateFmt:'HH:mm:ss'})</xsl:attribute> <xsl:attribute name="class">Wdate</xsl:attribute> </xsl:when> <xsl:otherwise></xsl:otherwise> </xsl:choose> <xsl:attribute name="style">operator:<xsl:value-of select="rows/queryRow/CUSTOMER_NAME/attribute::operator"></xsl:value-of>;dataType:<xsl:value-of select="rows/queryRow/CUSTOMER_NAME/attribute::dataType"></xsl:value-of>;</xsl:attribute> </xsl:element> </td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>