当前位置:   article > 正文

java中利用javax.xml.transform.Transformer将xml通过xsl样式转化成html_transformer.transform

transformer.transform

http://vbtboy.iteye.com/blog/619058

注意,需要你自己新建一个下面代码中出现的目录,

d:\study\xml2html\xsl     

d:\study\xml2html\xml

d:\study\xml2html\result


然后放入你写好的xsl文件和xml在对应的目录下面,运行main方法就可以了。


代码如下:

  1. package openjaw;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import javax.xml.transform.Transformer;
  6. import javax.xml.transform.TransformerFactory;
  7. import javax.xml.transform.stream.StreamResult;
  8. import javax.xml.transform.stream.StreamSource;
  9. public class XSLT_XML_HTML {
  10. public static String[] ExecuteXSL() {
  11. try {
  12. String[] xmlfiles = getXmlFilePath();
  13. String[] content = new String[xmlfiles.length];
  14. int i = 0;
  15. for (String xmlfile : xmlfiles) {
  16. ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
  17. TransformerFactory transformerFactory = TransformerFactory
  18. .newInstance();
  19. StreamSource source = new StreamSource(xmlfile);
  20. StreamResult result = new StreamResult(byteRep);
  21. StreamSource style = new StreamSource(getXslFilePath());
  22. Transformer transformer = transformerFactory
  23. .newTransformer(style);
  24. transformer.setOutputProperty(
  25. javax.xml.transform.OutputKeys.ENCODING, "utf-8"); // \u8BBE\u7F6E\u7F16\u7801
  26. transformer.transform(source, result);
  27. content[i] = byteRep.toString();
  28. i++;
  29. }
  30. return content;
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. return null;
  34. }
  35. }
  36. public static String getXslFilePath() {
  37. File file = new File("D:\\study\\xsl2xml\\xsl");
  38. File files[] = file.listFiles();
  39. String filename = "";
  40. String filepath = "";
  41. for (File f : files) {
  42. filename = f.getName();
  43. if (filename.indexOf("xsl") != -1) {
  44. filepath = f.getAbsolutePath();
  45. break;
  46. }
  47. }
  48. return filepath;
  49. }
  50. public static String[] getXmlFilePath() {
  51. File file = new File("D:\\study\\xsl2xml\\xml");
  52. File files[] = file.listFiles();
  53. String filename = "";
  54. String[] filepath = new String[files.length];
  55. for (int i = 0; i < files.length; i++) {
  56. File f = files[i];
  57. filename = f.getName();
  58. if (filename.indexOf("xml") != -1) {
  59. filepath[i] = f.getAbsolutePath();
  60. }
  61. }
  62. return filepath;
  63. }
  64. public static void createFile(String[] contents) {
  65. try {
  66. for (int i = 0; i < contents.length; i++) {
  67. File newFile = new File("D:\\study\\xsl2xml\\result\\" + i+".html");
  68. if (!newFile.exists()) {
  69. newFile.createNewFile();
  70. }
  71. FileWriter fw = new FileWriter(newFile);
  72. fw.write(contents[i]);
  73. fw.close();
  74. }
  75. } catch (Exception e) {
  76. // TODO: handle exception
  77. }
  78. }
  79. public static void main(String args[]) {
  80. String contents[] = ExecuteXSL();
  81. for (String content : contents) {
  82. System.out.println(content);
  83. }
  84. createFile(contents);
  85. }
  86. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/817557
推荐阅读
相关标签
  

闽ICP备14008679号