# coding: utf-8
from xml.dom import minidom
class XMLDemo:
def xml_to_list(self, xmlpath):
doc = minidom.parse(xmlpath) # 打开XML文档对象
root = doc.documentElement # 获取xml根元素
user_nodes = self.get_xmlnode(root, "tenant")
tenant_list = []
for node in user_nodes:
tenant_id = self.get_attrvalue(node, "id")
node_name = self.get_xmlnode(node, "name")
node_enable = self.get_xmlnode(node, "enable")
tenant_name = self.get_nodevalue(node_name[0])
tenant_enable = self.get_nodevalue(node_enable[0])
tenant = {}
tenant["id"], tenant["name"], tenant["enable"] = (tenant_id, tenant_name, tenant_enable)
tenant_list.append(tenant)
return tenant_list
def get_xmlnode(self, node, name):
return node.getElementsByTagName(name) if node else []
def get_attrvalue(self, node, attrname):
return node.getAttribute(attrname) if node else ""
def get_nodevalue(self, node, index=0):
return node.childNodes[index].nodeValue if node else ""
def xml_to_string(self, xmlpath):
doc = minidom.parse(xmlpath)
return doc.toxml("utf-8")
if __name__ == "__main__":
print XMLDemo().xml_to_list("../tmp/XMLDemo.xml")
print
print XMLDemo().xml_to_string("../tmp/XMLDemo.xml")
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Python2.7-Demo.zip (48个子文件)
Python2.7-Demo
JsonDemo.py 768B
入门100例
5.py 670B
20.py 452B
17.py 689B
23.py 672B
1.py 678B
13.py 700B
40.py 330B
19.py 671B
11.py 767B
30.py 518B
61.py 951B
38.py 897B
14.py 938B
21.py 672B
2.py 1KB
28.py 756B
4.py 702B
18.py 527B
37.py 740B
36.py 370B
24.py 489B
9.py 440B
10.py 372B
16.py 539B
15.py 475B
31.py 921B
39.py 857B
12.py 774B
3.py 701B
29.py 955B
6.py 272B
27.py 413B
25.py 449B
8.py 326B
22.py 619B
.idea
Python2.7-Demo.iml 284B
scopes
scope_settings.xml 143B
misc.xml 1KB
vcs.xml 164B
.name 14B
modules.xml 280B
encodings.xml 164B
workspace.xml 34KB
tmp
XMLDemo.xml 270B
JsonDemo.json 62B
XMLDemo.py 1KB
mysqlDemo.py 321B
共 48 条
- 1
资源评论
rickren729
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功