Python将xml和xsl转换为html的方法
作者:Sephiroth 时间:2022-04-02 14:24:32
本文实例讲述了Python将xml和xsl转换为html的方法。分享给大家供大家参考。具体分析如下:
这里需要用libxml2,所以还要先安装了libxml2模块才能使用。代码如下:
# -*- coding: mbcs -*-
#!/usr/bin/python
import libxml2, libxslt
class compoundXML:
def __init__(self):
self._result = None
self._xsl = None
self._xml = None
def do(self, xml_file_name, xsl_file_name):
self._xml = libxml2.parseFile(xml_file_name)
if self._xml == None:
return 0
styledoc = libxml2.parseFile(xsl_file_name)
if styledoc == None:
return 0
self._xsl = libxslt.parseStylesheetDoc(styledoc)
if self._xsl == None:
return 0
self._result = self._xsl.applyStylesheet(self._xml, None)
def get_xml_doc(self):
return self._result
def get_translated(self):
return self._result.serialize('UTF-8')
def save_translated(self, file_name):
self._xsl.saveResultToFilename(file_name, self._result, 0)
def release(self):
'''
this function must be called in the end.
'''
self._xsl.freeStylesheet()
self._xml.freeDoc()
self._result.freeDoc()
self._xsl = None
self._xml = None
self._result = None
if __name__ == '__main__':
test = compoundXML()
test.do('test/testxmlutil.xml', 'test/testxmlutil.xsl')
print test.get_translated()
test.save_translated('test/testxmlutil.htm')
test.release()
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,转换,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python利用keras接口实现深度神经网络回归
2021-02-07 12:01:41
![](https://img.aspxhome.com/file/2023/1/69581_0s.png)
python语法学习print中f-string用法示例
2021-01-08 11:11:24
我的ImageMagick使用心得
2008-10-21 11:05:00
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021122020655s.jpg)
php连接微软MSSQL(sql server)完全攻略
2023-07-16 17:56:43
![](https://img.aspxhome.com/file/2023/6/55426_0s.jpg)
Mootools 1.2教程(6)——操纵HTML DOM元素
2008-11-20 13:19:00
css实现图片倒影效果
2007-11-05 18:29:00
![](https://img.aspxhome.com/file/UploadPic/200711/5/2007115184648157s.jpg)
Python Django教程之实现天气应用程序
2022-03-22 23:02:43
![](https://img.aspxhome.com/file/2023/1/69011_0s.png)
简单介绍Python中的struct模块
2023-09-22 16:27:01
python数据处理之Pandas类型转换的实现
2021-04-11 11:17:36
![](https://img.aspxhome.com/file/2023/4/65864_0s.png)
python中的反斜杠问题深入讲解
2023-04-20 14:08:36
ASP中的Debug类--VBScript
2008-10-24 09:38:00
Flash的Fallback Content等
2010-04-01 12:18:00
HTML中小meta的大作用
2008-04-25 22:40:00
不要犯WEB字体编辑的10种错误
2008-08-19 12:55:00
![](https://img.aspxhome.com/file/UploadPic/20088/19/200881912588627s.gif)
Python绘画好看的星空图
2021-11-22 12:48:12
![](https://img.aspxhome.com/file/2023/7/86367_0s.gif)
python快速查找算法应用实例
2021-04-17 10:28:13
SQL Server 2000数据库FOR XML查询概述
2008-12-09 14:49:00
如何实现SQL Server的分页显示?
2010-05-18 18:36:00
python实现删除文件与目录的方法
2023-11-12 23:34:46
SQL Server树型结构数据处理的存储过程
2010-01-26 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20101/201012615399712s.jpg)