本文整理汇总了Python中lxml.etree.ElementTree.getpath方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.getpath方法的具体用法?Python ElementTree.getpath怎么用?Python ElementTree.getpath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.getpath方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: XMLRegistery
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import getpath [as 别名]
class XMLRegistery(object):
"""Represent a LifecycleManager in XML.
This is a singleton.
"""
_instance = None
_xml_root_tree = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(XMLRegistery, cls).__new__(cls, *args, **kwargs)
return cls._instance
def _xml_register(self, ressource, parent=None):
"""
:type ressource: XMLRessource
:type parent: lxml.Element
"""
attributes = {RESSOURCE_ATTR: ressource._xml_ressource_name()}
attributes.update(ressource._xml_attributes())
if parent is None:
xml_elt = Element(ressource._xml_tag(), attrib=attributes)
self._xml_root_tree = ElementTree(xml_elt)
else:
xml_elt = SubElement(parent,
ressource._xml_tag(),
attrib=attributes)
ressource._xpath = self._xml_root_tree.getpath(xml_elt)
try:
ressource._xpath_relative = ressource._xpath.split("/", 2)[2]
except IndexError:
ressource._xpath_relative = ressource._xpath
if (ressource._xml_add_properties()
or ressource._xml_add_properties_tuple()):
properties_node = SubElement(xml_elt, "properties")
for (prop, value) in ressource._xml_add_properties_tuple():
logger.trace("Add property '%s:%s' on node with tag %s" % (
prop, value, ressource._xml_tag()))
sub = SubElement(properties_node, prop)
sub.text = value
for elt in ressource._xml_add_properties():
logger.trace("Add property '%s' on node with tag %s" % (
elt.tag, ressource._xml_tag()))
properties_node.append(elt)
self._xml_register_children(xml_elt, ressource)
logger.trace("Registered %s in XML registery" % ressource.__repr__())
ressource._xml_on_registration()
def _xml_register_children(self, xml_elt, ressource):
"""Be careful, this removes children before adding them."""
# Children are removed to avoid multiple adding if the
# lifecycle is created several times.
for c in xml_elt.iterchildren():
if c.tag != "properties":
xml_elt.remove(c)
for c in ressource._xml_children():
self._xml_register(c, parent=xml_elt)
def to_string(self, xpath):
return tostring(self._find_one(xpath), pretty_print=True)
def xpath(self, xpath):
"""
:rtype: [str]
"""
acc = []
try:
request = self._xml_root_tree.xpath(xpath)
if type(request) != list:
return [str(request)]
for e in request:
if type(e) == _Element:
acc.append(tostring(e, pretty_print=True))
else:
acc.append(str(e))
except XPathEvalError:
raise XpathInvalidExpression("xpath '%s' is not valid!" % xpath)
return acc
def find_all_elts(self, xpath):
try:
return [self._xml_root_tree.getpath(e) for e in
self._xml_root_tree.xpath(xpath)]
except XPathEvalError:
raise XpathInvalidExpression("xpath '%s' is not valid!" % xpath)
def _find_one(self, xpath):
"""Return the ressource uri. Raise exception if multiple match
#.........这里部分代码省略.........