本文整理匯總了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