原始xml文档(下划线部分是要解析的数据):
<?xml version="1.0"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
解析为list:
test_datasets = []
import xml.dom.minidom
dom = xml.dom.minidom.parse('e:\MyDoc\Project\GODU_BVT\build\2011-01-18\GODU_WEB\build\result\cov\coverage-summary.xml')
raw_test_res = dom.getElementsByTagName('coverage')[0]
test_datasets.append(raw_test_res.attributes['lines-covered'].value)
test_datasets.append(raw_test_res.attributes['lines-valid'].value)
test_datasets.append(raw_test_res.attributes['line-rate'].value)
test_datasets.append(raw_test_res.attributes['branches-covered'].value)
test_datasets.append(raw_test_res.attributes['branches-valid'].value)
test_datasets.append(raw_test_res.attributes['branch-rate'].value)
test_datasets.append(raw_test_res.attributes['complexity'].value)
return test_datasets
解析为字典:
test_datasets = {}
import xml.dom.minidom
dom = xml.dom.minidom.parse('e:\MyDoc\Project\GODU_BVT\build\2011-01-18\GODU_WEB\build\result\cov\coverage-summary.xml')
raw_data_set = dom.getElementsByTagName('coverage')[0].attributes
for the_attr in raw_data_set.values():
test_datasets[the_attr.name] = the_attr.value
return test_datasets