Python xml 模块,dom() 实例源码
我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用xml.dom()。
def __init__(self, input_file):
self.dom = minidom.parse(input_file)
self.svg_node = self.dom.documentElement
def import_groups(self, from_svg_processor):
for child in from_svg_processor.svg_node.childNodes:
if child.nodeType != child.ELEMENT_NODE or child.tagName != 'g':
continue
group = child
output_node = self.dom.importNode(group, True)
self.svg_node.appendChild(output_node)
def wrap_with_group(self, attrs):
parent = self.svg_node
wrapper = self.dom.createElement("g")
for k,v in attrs.items():
wrapper.setAttribute(k,v)
for child in parent.getElementsByTagName('g'):
parent.removeChild(child)
wrapper.appendChild(child)
parent.appendChild(wrapper)
def text(dom, x):
return dom.getElementsByTagName('em:' + x)[0].childNodes[0].wholeText
def get_install(addon_file):
result = {'signed': False}
with zipfile.ZipFile(addon_file, 'r') as xpi:
dom = minidom.parse(xpi.open('install.rdf', 'r'))
for key in ['id', 'name', 'type', 'version']:
result[key] = text(dom, key)
if 'META-INF/mozilla.rsa' in xpi.namelist():
result['signed'] = True
return result
def element_from_xml_string(xml_string, element):
xml_as_dom = xml.dom.minidom.parseString(xml_string)
elements_by_name = xml_as_dom.getElementsByTagName(element)
element_value = None
if len(elements_by_name) > 0:
element_value = elements_by_name[0].toxml().replace('<' + element + '>', '').replace(
'</' + element + '>', '')
return element_value