使用dom4j实现XSL展示XML

来源:互联网 发布:现在学什么编程语言好 编辑:程序博客网 时间:2024/05/21 17:21

最近在给一个旧项目做报表,之前的报表太丑,交互性也不好,只能重做报表。项目的设计是从持久层返回一个xml文件,再使用xsl去展示xml数据。本来想从根本上去改持久层,可惜持久层被多个项目使用,无法改动,最后只能从xsl去着手,再重新用前台去展示。

项目里有很多的xsl文件,一个个修改再去跑场景,速度很慢,xsl写错了,还有可能导致页面挂掉,需要重新加载,开发速度太慢,我就自己写了一个java小程序去读取xml和xsl。

public class XSLUtil {private static String xmlPath = "sample.xml";private static String xslPath = "Lists.xsl";public static void main(String[] args) {XSLUtil d4j = new XSLUtil();String str = d4j.transformDocument(xmlPath, xslPath);System.out.println(str);}/** * 使用xsl转化xml * @param xmlPath * @param xslPath * @return */public String transformDocument(String xmlPath, String xslPath) {return this.transformDocument(new File(xmlPath), new File(xslPath));}/** * 使用xsl文件转化xml文件 * @param xmlFile * @param xslFile * @return */public String transformDocument(File xmlFile, File xslFile){String result = null;if(xmlFile == null || !xmlFile.exists() || xslFile == null || !xslFile.exists()){return result;}Document doc = null;try {doc = parseXmlFileToDocument(xmlFile);} catch (DocumentException e) {System.err.println("Cannot parse XML to Document.");e.printStackTrace();}Document resultDoc = null;try {resultDoc = this.transformDocument(doc, xslFile);} catch (TransformerException e) {System.err.println("Cannot parse doc with xsl.");e.printStackTrace();}result = parseDoc2String(resultDoc);return result;}/** * 将xml文件转换为Doc对象 * @param xmlFile * @return * @throws DocumentException */public Document parseXmlFileToDocument(File xmlFile)throws DocumentException {if (xmlFile != null) {Document doc = null;SAXReader reader = new SAXReader();doc = reader.read(xmlFile);return doc;}return null;}/** * 将xml转换为Doc对象 * @param xmlPath * @return * @throws DocumentException */public Document parseXmlFileToDocument(String xmlPath)throws DocumentException {File file = new File(xmlPath);return this.parseXmlFileToDocument(file);}/** * 使用xsl文件转化xml文件 * @param doc * @param xslFile * @return * @throws TransformerException */public Document transformDocument(Document doc, File xslFile) throws TransformerException {if(doc == null || !xslFile.exists() || xslFile == null){return null;}TransformerFactory factory = TransformerFactory.newInstance();Document transformerDoc = null;Transformer transformer = factory.newTransformer(new StreamSource(xslFile));DocumentSource docSource = new DocumentSource(doc);DocumentResult docResult = new DocumentResult();transformer.transform(docSource, docResult);transformerDoc = docResult.getDocument();return transformerDoc;}/** * 将doc转为string * @param transformDoc * @return */private String parseDoc2String(Document transformDoc) {if(transformDoc == null){return null;}StringWriter strWriter = new StringWriter();OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");format.setExpandEmptyElements(false);XMLWriter xmlWriter = new XMLWriter(strWriter, format);try {xmlWriter.write(transformDoc);xmlWriter.flush();} catch (IOException e) {e.printStackTrace();}return strWriter.toString();}}


0 0
原创粉丝点击