本文整理汇总了Python中suds.sax.element.Element.childAtPath方法的典型用法代码示例。如果您正苦于以下问题:Python Element.childAtPath方法的具体用法?Python Element.childAtPath怎么用?Python Element.childAtPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类suds.sax.element.Element
的用法示例。
在下文中一共展示了Element.childAtPath方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from suds.sax.element import Element [as 别名]
# 或者: from suds.sax.element.Element import childAtPath [as 别名]
class Document:
""" An XML Document """
DECL = '<?xml version="1.0" encoding="UTF-8"?>'
def __init__(self, root=None):
"""
@param root: A root L{Element} or name used to build
the document root element.
@type root: (L{Element}|str|None)
"""
self.__root = None
self.append(root)
def root(self):
"""
Get the document root element (can be None)
@return: The document root.
@rtype: L{Element}
"""
return self.__root
def append(self, node):
"""
Append (set) the document root.
@param node: A root L{Element} or name used to build
the document root element.
@type node: (L{Element}|str|None)
"""
if isinstance(node, basestring):
self.__root = Element(node)
return
if isinstance(node, Element):
self.__root = node
return
def getChild(self, name, ns=None, default=None):
"""
Get a child by (optional) name and/or (optional) namespace.
@param name: The name of a child element (may contain prefix).
@type name: basestring
@param ns: An optional namespace used to match the child.
@type ns: (I{prefix}, I{name})
@param default: Returned when child not-found.
@type default: L{Element}
@return: The requested child, or I{default} when not-found.
@rtype: L{Element}
"""
if self.__root is None:
return default
if ns is None:
prefix, name = splitPrefix(name)
if prefix is None:
ns = None
else:
ns = self.__root.resolvePrefix(prefix)
if self.__root.match(name, ns):
return self.__root
else:
return default
def childAtPath(self, path):
"""
Get a child at I{path} where I{path} is a (/) separated
list of element names that are expected to be children.
@param path: A (/) separated list of element names.
@type path: basestring
@return: The leaf node at the end of I{path}
@rtype: L{Element}
"""
if self.__root is None:
return None
if path[0] == '/':
path = path[1:]
path = path.split('/',1)
if self.getChild(path[0]) is None:
return None
if len(path) > 1:
return self.__root.childAtPath(path[1])
else:
return self.__root
def childrenAtPath(self, path):
"""
Get a list of children at I{path} where I{path} is a (/) separated
list of element names that are expected to be children.
@param path: A (/) separated list of element names.
@type path: basestring
@return: The collection leaf nodes at the end of I{path}
@rtype: [L{Element},...]
"""
if self.__root is None:
return []
if path[0] == '/':
path = path[1:]
path = path.split('/',1)
if self.getChild(path[0]) is None:
return []
if len(path) > 1:
return self.__root.childrenAtPath(path[1])
#.........这里部分代码省略.........
示例2: test_missing
# 需要导入模块: from suds.sax.element import Element [as 别名]
# 或者: from suds.sax.element.Element import childAtPath [as 别名]
def test_missing(self):
root = Element("root")
result = root.childAtPath("an invalid path")
assert result is None