本文整理汇总了Python中xml.etree.cElementTree方法的典型用法代码示例。如果您正苦于以下问题:Python etree.cElementTree方法的具体用法?Python etree.cElementTree怎么用?Python etree.cElementTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree
的用法示例。
在下文中一共展示了etree.cElementTree方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_attribute_on_first_child
# 需要导入模块: from xml import etree [as 别名]
# 或者: from xml.etree import cElementTree [as 别名]
def set_attribute_on_first_child(docfrag, name, value, treeName):
"""naively sets an attribute on the first child of the document
fragment passed in"""
setter = {'ElementTree': lambda d: d[0].set,
'DOM': lambda d: d.firstChild.setAttribute}
setter['cElementTree'] = setter['ElementTree']
try:
setter.get(treeName, setter['DOM'])(docfrag)(name, value)
except AttributeError:
setter['ElementTree'](docfrag)(name, value)
示例2: get_etree
# 需要导入模块: from xml import etree [as 别名]
# 或者: from xml.etree import cElementTree [as 别名]
def get_etree():
global _etree
if _etree is not None:
return _etree
try:
from lxml import etree as _etree
except ImportError:
try:
from xml.etree import cElementTree as _etree
except ImportError:
try:
from xml.etree import ElementTree as _etree
except ImportError:
raise TypeError('lxml or etree not found')
return _etree