Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140606
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: Python/Ruby

2017-03-01 09:46:50


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author: Dily
  4. #  Email: 312797697@qq.com
  5. """实现XML转HTML"""
  6.  
  7. import libxml2, libxslt
  8. import sys
  9. import time

  10. mytime = time.strftime('%Y-%m-%d-%H-%M.html',time.localtime(time.time()))
  11.  
  12. class compoundXML:
  13.     def __init__(self):
  14.         self._result = None
  15.         self._xsl = None
  16.         self._xml = None
  17.            
  18.     def do(self, xml_file_name, xsl_file_name):
  19.         self._xml = libxml2.parseFile(xml_file_name)
  20.         if self._xml == None:
  21.             return 0
  22.         styledoc = libxml2.parseFile(xsl_file_name)
  23.         if styledoc == None:
  24.             return 0
  25.         self._xsl = libxslt.parseStylesheetDoc(styledoc)
  26.         if self._xsl == None:
  27.             return 0

  28.         self._result = self._xsl.applyStylesheet(self._xml, None)
  29.         
  30.     def get_xml_doc(self):
  31.         return self._result
  32.         
  33.         
  34.     def get_translated(self): 
  35.         return self._result.serialize('utf-8')
  36.     
  37.     def save_translated(self, file_name):
  38.        self._xsl.saveResultToFilename(file_name, self._result, 0)
  39.         
  40.     def release(self):
  41.         '''
  42.         this function must be called in the end.
  43.         '''
  44.         self._xsl.freeStylesheet()
  45.         self._xml.freeDoc()
  46.         self._result.freeDoc()
  47.         self._xsl = None
  48.         self._xml = None
  49.         self._result = None

  50. def main():
  51.     if len(sys.argv) < 2:
  52.         print '''\
  53.     No action specified.
  54.     Detailed inquiry help
  55.     eg. : python xmltohtml.py --help'''
  56.         sys.exit()

  57.     if sys.argv[1].startswith('--'):
  58.         option = sys.argv[1][2:]
  59.         if option == 'version':
  60.             print 'Version 1.0'
  61.         elif option == 'help':
  62.             print '''\
  63.     This program prints files to the standard output or time name files.
  64.     Two number of files can be specified.
  65.     Options include:
  66.       --version : Prints the version number
  67.       --help : Display this help
  68.         argv[1] : xml file
  69.         argv[2] : xsl file
  70.         eg. : python xmltohtml.py stat.xml stat.xsl'''
  71.         else:
  72.             print '''\
  73.     Unknown option.
  74.     Detailed inquiry help
  75.     eg. : python xmltohtml.py --help'''
  76.         sys.exit()
  77.     else:
  78.         xml = compoundXML()
  79.         xml.do(sys.argv[1], sys.argv[2])
  80.         print xml.get_translated()
  81.         xml.save_translated(mytime)
  82.         xml.release()

  83. if __name__ == '__main__':
  84.     main()

阅读(1893) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~