try
{
  TransformerFactory tf = TransformerFactory.newInstance();
  Source xml = new DOMSource(doc);
  Source xsl = new StreamSource(xslfile);
  Result html = new StreamResult(htmlfile);
  Transformer tran = tf.newTransformer(xsl);
  tran.transform(xml,html);
}
catch (TransformerConfigurationException e)
{
}
catch (TransformerException e)
{
}

解决方案 »

  1.   

    import java.io.*;
    import java.util.Locale;
    import java.net.*;
    import org.w3c.dom.*;
    import oracle.xml.parser.v2.*;public class XSLTSample {
      public static void main (String args[]) throws Exception {
        try {
          
          DOMParser parser = new DOMParser();      
          URL xmlURL = createURL("sample1.xml");
          URL xslURL = createURL("sample1.xsl");
        
          parser.parse(xmlURL);
          XMLDocument xmldoc = parser.getDocument();
         
          parser.parse(xslURL);
          XMLDocument xsldoc = parser.getDocument();      XSLStylesheet stylesheet = new XSLStylesheet(xsldoc, xslURL);      XSLProcessor processor = new XSLProcessor();      processor.setErrorStream(System.err);      processor.processXSL(stylesheet, xmldoc, System.out);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
      static URL createURL(String fileName) {
        URL url = null;
        try {
          url = new URL(fileName);
        }
        catch (MalformedURLException ex) {
          File f = new File(fileName);
          try {
            String path = f.getAbsolutePath();
            String fs = System.getProperty("file.separator");
            if (fs.length() == 1) {
              char sep = fs.charAt(0);
              if (sep != '/')
                path = path.replace(sep, '/');
              if (path.charAt(0) != '/')
                path = '/' + path;
            }
            path = "file://" + path;
            url = new URL(path);
          }
          catch (MalformedURLException e) {
            System.out.println("Cannot create url for: " + fileName);
            System.exit(0);
          }
        }
        return url;
      }
    }
      

  2.   

    <?xml version="1.0"?>
    <?xml-stylesheet href="scores.xsl" type="text/xsl"?>
    <?xml-stylesheet href="scores_wap.xsl" type="text/xsl" media="phone"?>
    <?xml-stylesheet href="scores_print.xsl" type="text/xsl" media="print"?>
    <results showName="New Port Richey Open" location="New Port Richey, FL" showDate="7/31/01">
    ...
    </results>
    import java.io.File;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.Source;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;public class ChooseStyleSheet {
       public static void main (String args[]) {
          String rootName = null;
          try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document doc = db.parse("scores.xml");
             Element root = doc.getDocumentElement();
             rootName = root.getNodeName();
          } catch (Exception e) {
             e.printStackTrace();
          }      try {         TransformerFactory transformerFactory =
                              TransformerFactory.newInstance();         StreamSource source = new StreamSource("scores.xml");
             StreamResult result = new StreamResult("result.xml");         String styleName = null;
             if (rootName.equals("results")){
                 styleName = "scores.xsl";
             } else if (rootName.equals("entries")){
                 styleName = "entries.xsl";
             }         Transformer transformer = null;
             if (styleName == null) {             transformer = transformerFactory.newTransformer();         } else {             StreamSource style = new StreamSource(styleName);
                 transformer =  transformerFactory.newTransformer(style);         }         transformer.transform(source, result);      } catch (Exception e) {
    e.printStackTrace();
          }
       }
    }
      

  3.   


    http://expert.csdn.net/Expert/TopicView1.asp?id=1562645
      public static String transXML2HTML(String xml,String xslfile){
        StringWriter sw=null;
        if(xml.equals("")||xml==null) return "<H1>无该满足条件的记录</H1>";    try {
          String strOut = new String(xml.getBytes("gbk"),"iso8859-1");
          StringBufferInputStream sbis=new StringBufferInputStream(strOut);      StreamSource sXML=new StreamSource();
          sXML.setInputStream(sbis);
          File file=new File(xslfile);
          StreamSource sSTL=new StreamSource(file);      TransformerFactory tFactory = TransformerFactory.newInstance();
          Transformer transformer = tFactory.newTransformer(sSTL);      sw=new StringWriter();
          StreamResult sr=new StreamResult(sw);
          transformer.setOutputProperty("method","html");
          transformer.transform(sXML,sr);
        }
        catch (Exception e) {
          System.out.println("TransXML2HTML error:"+e);
        }
        return sw.getBuffer().toString();
      }