DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

用minidom解析xml文档


原始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



Published

Jan 18, 2011

Last Updated

Jan 18, 2011

Category

Tech

Tags

  • 解析 2
  • minidom 1
  • Python 136

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor