將xml文件按照xsl文件樣式快速生成html文件的方法

日誌 > 個人日記
設置置頂 | 編輯 | 刪除

將xml文件按照xsl文件樣式快速生成html文件的方法

發表於:2008年2月19日 11時38分42秒閱讀(3)評論(1)本文鏈接:http://user.qzone.qq.com/592433424/blog/1203392322
package com.shzscq.searchpatent.parser;
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);
}
}
 
評論列表
聲明:騰訊不會在QQ空間以回覆方式通知用戶中獎,請大家不要相信這些虛假信息以免上當受騙。更多虛假案例請點擊->
用戶頭像
發紙條
加好友
主人回覆 | 引用 | 舉報 | 刪除1樓  2008年2月19日 11時42分39秒

好方法,送花
主人回覆:
 
請選擇道具
<textarea class="content" id="commentEditor" style="BORDER-RIGHT: #ccc 1px solid; BORDER-TOP: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; COLOR: gray! important; BORDER-BOTTOM: #ccc 1px solid" onfocus="getUBBeditor(this)" rows="13" cols="50" name="content">點擊這裏發表評論</textarea>
溫馨提示:點擊驗證碼輸入框,以獲取驗證碼
請輸入驗證碼:
     
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章