用Transformer在服务端就转成html文件,
这个论坛里有很多例子。

解决方案 »

  1.   

    import javax.xml.transform.*;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource; /**
       * 这个方法将xml通过样式单转换.
       * @param xmlfile 将要被转换的xml文件路径
       * @param xslfile XSL文件的文件路径
       * @return String 转换后的字符串形式存放的html
       */
      public static String ExecuteXSL(String xmlfile, String xslfile) {
        try {
          ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
          TransformerFactory transformerFactory =
              TransformerFactory.newInstance();
          StreamSource source = new StreamSource(xmlfile);
          StreamResult result = new StreamResult(byteRep);
          StreamSource style = new StreamSource(xslfile);
          Transformer transformer =
              transformerFactory.newTransformer(style);
          transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,
                                        "GB2312"); //\u8BBE\u7F6E\u7F16\u7801
          transformer.transform(source, result);
          return byteRep.toString();
        }
        catch (Exception e) {
          e.printStackTrace();
          return null;
        }  }
      

  2.   

    楼上的兄弟,不行啊,
    我的xsl如下:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
      <xsl:template match="/">
        <HTML>
          <HEAD>
            <TITLE><xsl:value-of select="document/title"/></TITLE>
          </HEAD>
          <BODY>
            <H1><xsl:value-of select="document/title"/></H1>
            <xsl:apply-templates select="document/section"/>
          </BODY>
        </HTML>
      </xsl:template>
      <xsl:template match="section">
        <DIV>
          <H2><xsl:value-of select="title"/></H2>
          <xsl:apply-templates />
        </DIV>
      </xsl:template>  <xsl:template match="p">
        <P><xsl:apply-templates /></P>
      </xsl:template>  <xsl:template match="list">
        <UL>
          <xsl:for-each select="item">
            <LI><xsl:apply-templates /></LI>
          </xsl:for-each>
        </UL>
      </xsl:template>  <xsl:template match="emph">
        <I><xsl:apply-templates /></I>
      </xsl:template>    <xsl:template match="text()"><xsl:value-of /></xsl:template>
      
    </xsl:stylesheet>
    xml如下:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="pole.xsl"?>
    <document>
      <title>To the Pole and Back</title>
      <section>
        <title>The First Day</title>
        <p>It was the <emph>best</emph> of days, it was the
          <emph>worst</emph> of days.</p>
        <list>
          <item><emph>best</emph> in that the sun was out.</item>
          <item><emph>worst</emph> in that it was 39 degrees below zero.</item>
        </list>
        <section>
          <title>Lunch Menu</title>
          <list>
            <item>ice cream</item>
            <item>popsicles</item>
          </list>
        </section>  </section>  <section>
        <title>The Second Day</title>
        <p>Ditto the first day.</p>
      </section></document>
    楼上的兄弟可以试试