一个servlet中,获取了一个字符串,里面的内容是xml字符串,如下面:<note><id>1</id><title>test2</title><link>http://www.google.com/</link><description>desc</description><html>texts</html><section_id>1</section_id><order>1</order></note><note><id>3</id><title>prototype</title><link>prototype</link><description>prototype</description><html>test</html><section_id>1</section_id><order>3</order></note><note><id>5</id><title>jquery</title><link>jquery.com</link><description>new wave of javascript</description><html>test jquery</html><section_id>1</section_id><order>2</order></note><note><id>11</id><title>test</title><link>http://www.sina.com.cn/</link><description>sinaa</description><html>dksldkfdsdf</html><section_id>1</section_id><order>4</order></note><note><id>21</id><title>test</title><link>http://www.google.com/</link><description>google</description><html>test</html><section_id>10</section_id><order>2</order></note><note><id>22</id><title>test2</title><link>http://www.yahoo.com/</link><description>yahoo</description><html>test</html><section_id>10</section_id><order>1</order></note><note><id>23</id><title>java</title><link>http://www.javaeye.net</link><description>test</description><html>dskdlkfsdf</html><section_id>5</section_id><order>1</order></note>
现在我想把它通过样式xsl直接显示为html,应该怎么做呢?谢谢!我遇到的问题是,不知道如何处理这段xml,我不想把它存为xml文件中,也不用第三方类库能做到吗?谢谢

解决方案 »

  1.   

    亲,JDK1.5 还是 1.6的有个方法是通过代码是可以把XML转换成html的,转换完直接printWrite.print(string)就可以了
      

  2.   

    [java]
    /**
     * 动态生成HTM,从XML转换成HTM,并返回HTM的内容
     * @param xmlFileName
     * @param xslFileName
     * @param htmlFileName
     * @return
     */
    public static String Transform(String xmlFileName, String xslFileName,
                String htmlFileName) {
    String htmlString = "";
            try {
                TransformerFactory tFac = TransformerFactory.newInstance();
                Source xslSource = new StreamSource(xslFileName);
                Transformer t = tFac.newTransformer(xslSource);
                File xmlFile = new File(xmlFileName);
                File htmlFile = new File(htmlFileName);
                Source source = new StreamSource(xmlFile);
                Result result = new StreamResult(htmlFile);
                t.transform(source, result);
                
          File file = new File(htmlFileName);
            BufferedReader reader = null;
                reader = new BufferedReader(new FileReader(file));
                String tempString = null;
                // 一次读入一行,直到读入null为文件结束
                while ((tempString = reader.readLine()) != null) {
                    // 显示行号
                    htmlString += tempString+"\n";
                }
                reader.close();
                
                //System.out.print(htmlString);
                
            } catch (TransformerConfigurationException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            } catch ( IOException e) {
             e.printStackTrace();
            }
            
            return htmlString;
        }
    [/java]
      

  3.   

    .Transform2(xmlFile, xslFile, out);out.flush();
    out.close();