javaWEB项目中使用XSLT解析生成的XML文件

        XSLT方式打开XML文件,在客户端运用时,

一可以编辑好XSLT文件,在XML文件导入XSLT文件就可以用浏览器打开:

<?xml-stylesheet type="text/xsl" href="xml.xsl"?>

二可以使用js打开:

<script type="text/javascript">

// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")

// Transform
document.write(xml.transformNode(xsl))

</script>
但2种方式只支持IE5以上,包括谷歌等非IE浏览器不起作用。


        现在需要在javaWEB程序中,在action类里使用XSLT解析XML文件并显示出来:

jsp页面直接用jquery-ajax异步请求调用到strugts2的action,显示时候不一定要新打开html页面,可以在div用innerHTML直接显示,会自动识别HTML表格标签等,这里不再写出来。

下面只写action类文件程序代码:

	/**
	 * @return
	 * @throws IOException
	 * @throws SQLException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws DocumentException
	 * @throws TransformerException 
	 */
	public void showXmlAndXslt() throws IOException, SQLException, SAXException, ParserConfigurationException, DocumentException, TransformerException{
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/xml;charset=utf-8"); //(1)一定要在(2)的前面,不然会乱码  
	    response.setCharacterEncoding("UTF-8"); //(2)  
	    response.setHeader("Cache-Control", "no-cache");   
	    PrintWriter pw=response.getWriter();  
	    
	    String canshu1=request.getParameter("参数1");
	    String canshu2=request.getParameter("参数2");
	    
	    JavBean javaBean=xmlService.showXML(canshu1, canshu2);
	    
	    Clob clob=javaBean.getClobXml();
	      
	    //CLOB转String  
        Reader inStreamDoc = clob.getCharacterStream();       
        char[] tempDoc = new char[(int) clob.length()];       
        inStreamDoc.read(tempDoc);       
        inStreamDoc.close();       
          
        String msgXML= new String(tempDoc);  
          
        //msgContent="<?"+"xml-stylesheet"+" type=\"text/xsl\""+" href=\"xml.xsl\""+" ?>"+msgContent;
          
        //保存到D盘生成xml文件
          BufferedWriter bw = null;
          OutputStreamWriter osw = null;
          FileOutputStream fos = null;
          
          File xmlFile = new File("D:/user.xml");
          
          fos = new FileOutputStream(xmlFile);
          osw = new OutputStreamWriter(fos,"GBK");//指定编码,防止写中文乱码
          bw = new BufferedWriter(osw);
          
          //对xml输出格式化
          OutputFormat format = OutputFormat.createPrettyPrint();
          format.setEncoding("GBK");
          XMLWriter writer = new XMLWriter(bw, format);
          Document document=DocumentHelper.parseText(msgXML);
          writer.write(document);
          //writer.toString();
          //writer.flush();
          writer.close();   //一定要加,不然写不到xml文件,为0kb
         
          
        //XSLT解析xml																		
        //1.此种是在前面一步保存到硬盘D盘,对xml输出格式化的前提下:
        SAXReader saxReader=new SAXReader();
		Document document1=saxReader.read(new File("D:\\user.xml"));     //dom4j读取xml文件转换为Document		
		
		//2.此种是不需要保存到硬盘的前提下:(则前一步的保存D盘生成xml文件和对xml输出格式化可省略)													 
		ByteArrayInputStream is = new ByteArrayInputStream(msgXML.getBytes("utf-8"));  
        Document document1=saxReader.read(is);			
            
		TransformerFactory factory=TransformerFactory.newInstance();
		StreamSource xsl=new StreamSource("D:\\xml.xsl");
		Transformer transformer=factory.newTransformer(xsl);
		Properties props=transformer.getOutputProperties();
		props.setProperty(OutputKeys.ENCODING,"GBK");
		props.setProperty(OutputKeys.METHOD, "html");
		props.setProperty(OutputKeys.VERSION, "6.0");
		
		transformer.setOutputProperties(props);
		DocumentSource docSource=new DocumentSource(document1);
		StringWriter strWriter=new StringWriter();
		StreamResult docResult=new StreamResult(strWriter);
		transformer.transform(docSource, docResult);
          
	    StringBuilder sb=new StringBuilder();  
	    sb.append(strWriter);
	    
	    pw.print(sb.toString());  
	    pw.flush();  
	    pw.close(); 
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值