您的位置:首页 > 编程语言 > Java开发

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

2013-05-03 13:33 756 查看
        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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐