FileOutputStream fos = new FileOutputStream("D:\\MyCodes\\student.html");
        Result result = new StreamResult();
 Result result = new StreamResult(fos);

解决方案 »

  1.   

    public class test { public static void main(String[] args)throws Exception {

    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document doc=db.parse(test.class.getResourceAsStream("/customer_dtd.xml"));



           
            // Get Template
            FileInputStream fis1 = new FileInputStream("D:/customer.xsl");
            Source template = new StreamSource(fis1);
            
            FileOutputStream fos = new FileOutputStream("D:/student.html");
            Result result = new StreamResult(fos);
            
            Transformer transformer = TransformerFactory.newInstance().newTransformer(template);
            transformer.transform(new DOMSource(doc), result);
            
            fos.close();
            fis1.close();
           
    }
    }
      

  2.   

    xml文件:
    <?xml version="1.0" encoding="UTF-8"?><customers>
    <customer>
    <customerID>a1</customerID>
    <customerName>张三</customerName>
    <sex>男</sex>
    <buySet customerID="a1">
    <buy>
    <buyName>香蕉</buyName>
    <amount>100</amount>
    <price>100 </price>

    </buy>
    </buySet>
    </customer>


    <customer>
    <customerID>a2</customerID>
    <customerName>李四</customerName>
    <sex>男</sex>
    <buySet customerID="a2">
    <buy>
    <buyName>苹果</buyName>
    <amount>100</amount>
    <price>100</price>

    </buy>
    </buySet>
    </customer></customers>
      

  3.   

    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>
    <title></title>
    </head>
    <body>
    <table>
    <tbody>
    <tr>
    <th>编号</th>
    <th>姓名</th>
    <th>性别</th>
    </tr>
    <xsl:apply-templates select="customers/customer"></xsl:apply-templates>
    </tbody>
    </table>



    </body>
    </html></xsl:template>
    <xsl:template match="customers/customer">
    <tr>
    <td><xsl:value-of select="customerID"></xsl:value-of></td>
    <td><xsl:value-of select="customerName"></xsl:value-of></td>
    <td><xsl:value-of select="sex"></xsl:value-of></td>
    </tr></xsl:template>
    </xsl:stylesheet>
      

  4.   


     FileInputStream fis = new FileInputStream("D:\\MyCodes\\student.xml");
            Source source = new StreamSource(fis);
            
            // Get Template
            FileInputStream fis1 = new FileInputStream("D:\\MyCodes\\TestTemplate.xsl");
            Source template = new StreamSource(fis1);
            
            FileOutputStream fos = new FileOutputStream("D:\\MyCodes\\student.html");
            Result result = new StreamResult(fos);
            
            Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
            transformer.transform(template, result);
            
            fos.close();
            fis1.close();
            fis.close();<xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="D:/MyCodes/TestTemplate.xsl"?>
    <customers>
        <customer>
            <customerID>a1</customerID>
            <customerName>张三</customerName>
            <sex>男</sex>
            <buySet customerID="a1">
                <buy>
                    <buyName>香蕉</buyName>
                    <amount>100</amount>
                <price>100    </price>
                                        
                </buy>
            </buySet>
        </customer>
        
        
        <customer>
            <customerID>a2</customerID>
            <customerName>李四</customerName>
            <sex>男</sex>
            <buySet customerID="a2">
                <buy>
                    <buyName>苹果</buyName>
                    <amount>100</amount>
                <price>100</price>
                                        
                </buy>
            </buySet>
        </customer></customers>
      

  5.   

    错了FileInputStream fis = new FileInputStream("E:/source.xml");
    Source source = new StreamSource(fis);
    // Get Template
    FileInputStream fis1 = new FileInputStream("E:/temp.xsl");
    Source template = new StreamSource(fis1);
            
    Result result = new StreamResult(System.out);//注意 xslt 和 xml的该放的地方
    Transformer transformer = TransformerFactory.newInstance().newTransformer(template);
    transformer.transform(source, result);fis1.close();
    fis.close();<?xml version="1.0" encoding="GB2312" standalone="no"?>
    <!--注意这一行-->
    <?xml-stylesheet type="text/xsl" href="E:/temp.xsl"?>
    <root>
        <student-list>
            <student>
                <id>1</id>
                <name>张三</name>
                <address>东莞市育兴路2号</address>
            </student>
            <student>
                <id>2</id>
                <name>李四</name>
                <address>桂阳县欧阳海路4号</address>
            </student>
        </student-list>
    </root>
      

  6.   

    答对了我把 xml 和 xsl 模板搞反了
    JDK的文档中只说要传入什么类型的参数,也没提到哪个参数是模板,哪个参数是xml输入,害得我好苦