import java.io.File; import java.util.Properties; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; /** * 使用JAXP根據XSL轉換XML文檔 * JAXP是Java API for XML Processing的英文字頭縮寫, * 中文含義是:用於XML文檔處理的使用Java語言編寫的編程接口。 * JAXP支持DOM、SAX、XSLT等標準。 */ public class JAXPTransform { /** * 使用XSLT將XML文檔轉換成HTML * @param xmlFileName 源XML文件名 * @param xslFileName XSL文件名 * @param htmlFileName 輸出的HTML文件名 * @return 返回HTML文件名 */ public static String xml_xslt_html(String xmlFileName, String xslFileName, String htmlFileName)throws Exception{ // 創建XSLT引擎的工廠 TransformerFactory tFactory = TransformerFactory.newInstance(); // 創建XSLT引擎要使用的XSL文件源 StreamSource source = new StreamSource(new File(xslFileName)); // 創建XSLT引擎 Transformer tx = tFactory.newTransformer(source); // 設置XSLT引擎的輸出屬性,使之輸出爲HTML格式,並且支持中文。 Properties properties = tx.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING,"GB2312"); properties.setProperty(OutputKeys.METHOD, "html"); tx.setOutputProperties(properties); // 創建XML文件源和HTML文件的結果流 StreamSource xmlSource = new StreamSource(new File(xmlFileName)); File targetFile = new File(htmlFileName); StreamResult result = new StreamResult(targetFile); // 實現XSLT轉換,根據XSL文件源將XML文件源轉換成HTML結果流 tx.transform(xmlSource, result); return targetFile.getAbsolutePath(); } public static void main(String[] args) throws Exception { String xmlFileName = "D://Users//honghong1//workspace//SearchPatentNew//WebRoot//search//searchData.xsl"; String xslFileName = "D://Users//honghong1//workspace//SearchPatentNew//WebRoot//search//searchData.xml"; String targetFileName = "C://Users//honghong//Desktop//searchData.html"; JAXPTransform.xml_xslt_html(xmlFileName, xslFileName, targetFileName); } } |
將xml文件按照xsl文件樣式快速生成html文件的方法
日誌 > 個人日記
評論列表
loading......
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
共1篇評論,第1頁/共1頁
好方法,送花
共1篇評論,第1頁/共1頁