本文整理汇总了Python中xml.etree.ElementTree.iselement方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.iselement方法的具体用法?Python ElementTree.iselement怎么用?Python ElementTree.iselement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.iselement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_element
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def check_element(element):
if not ET.iselement(element):
print "not an element"
if not hasattr(element, "tag"):
print "no tag member"
if not hasattr(element, "attrib"):
print "no attrib member"
if not hasattr(element, "text"):
print "no text member"
if not hasattr(element, "tail"):
print "no tail member"
check_string(element.tag)
check_mapping(element.attrib)
if element.text is not None:
check_string(element.text)
if element.tail is not None:
check_string(element.tail)
for elem in element:
check_element(elem)
# --------------------------------------------------------------------
# element tree tests
示例2: get
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def get(item, default=None):
"""Get item from value (value[item]).
If the item is not found, return the default.
Handles XML elements, regex matches and anything that has __getitem__.
"""
def getter(value):
if ET.iselement(value):
value = value.attrib
try:
# Use .group() if this is a regex match object
if _is_re_match(value):
return value.group(item)
else:
return value[item]
except (KeyError, IndexError):
return default
except (TypeError, AttributeError) as err:
raise ValueError(err)
return transform(getter)
示例3: __format_element
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def __format_element(elt_data):
"""
Private method which ensures that a XML portion to be parsed is
of type xml.etree.ElementTree.Element.
If elt_data is a string, then it is converted to an
XML Element type.
:param elt_data: XML Element to be parsed or string
to be converted to a XML Element
:return: Element
"""
if isinstance(elt_data, str):
try:
xelement = ET.fromstring(elt_data)
except:
raise NmapParserException("Error while trying "
"to instanciate XML Element from "
"string {0}".format(elt_data))
elif ET.iselement(elt_data):
xelement = elt_data
else:
raise NmapParserException("Error while trying to parse supplied "
"data: unsupported format")
return xelement
示例4: SetXmlBlob
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def SetXmlBlob(self, blob):
"""Sets the contents of the extendedProperty to XML as a child node.
Since the extendedProperty is only allowed one child element as an XML
blob, setting the XML blob will erase any preexisting extension elements
in this object.
Args:
blob: str, ElementTree Element or atom.ExtensionElement representing
the XML blob stored in the extendedProperty.
"""
# Erase any existing extension_elements, clears the child nodes from the
# extendedProperty.
self.extension_elements = []
if isinstance(blob, atom.ExtensionElement):
self.extension_elements.append(blob)
elif ElementTree.iselement(blob):
self.extension_elements.append(atom._ExtensionElementFromElementTree(
blob))
else:
self.extension_elements.append(atom.ExtensionElementFromString(blob))
示例5: __SendDataPart
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def __SendDataPart(data, connection):
"""This method is deprecated, use atom.http._send_data_part"""
deprecated('call to deprecated function __SendDataPart')
if isinstance(data, str):
#TODO add handling for unicode.
connection.send(data)
return
elif ElementTree.iselement(data):
connection.send(ElementTree.tostring(data))
return
# Check to see if data is a file-like object that has a read method.
elif hasattr(data, 'read'):
# Read the file and send it a chunk at a time.
while 1:
binarydata = data.read(100000)
if binarydata == '': break
connection.send(binarydata)
return
else:
# The data object was not a file.
# Try to convert to a string and send the data.
connection.send(str(data))
return
示例6: CalculateDataLength
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def CalculateDataLength(data):
"""Attempts to determine the length of the data to send.
This method will respond with a length only if the data is a string or
and ElementTree element.
Args:
data: object If this is not a string or ElementTree element this funtion
will return None.
"""
if isinstance(data, str):
return len(data)
elif isinstance(data, list):
return None
elif ElementTree.iselement(data):
return len(ElementTree.tostring(data))
elif hasattr(data, 'read'):
# If this is a file-like object, don't try to guess the length.
return None
else:
return len(str(data))
示例7: xml_find
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def xml_find(xpath):
"""Find a XML element via xpath."""
def xpath_find(value):
validate(ET.iselement, value)
value = value.find(xpath)
if value is None:
raise ValueError("XPath '{0}' did not return an element".format(xpath))
return validate(ET.iselement, value)
return transform(xpath_find)
示例8: xml_findall
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def xml_findall(xpath):
"""Find a list of XML elements via xpath."""
def xpath_findall(value):
validate(ET.iselement, value)
return value.findall(xpath)
return transform(xpath_findall)
示例9: validate_xml_element
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def validate_xml_element(schema, value):
validate(ET.iselement, value)
new = ET.Element(value.tag, attrib=value.attrib)
if schema.attrib is not None:
try:
new.attrib = validate(schema.attrib, value.attrib)
except ValueError as err:
raise ValueError("Unable to validate XML attributes: {0}".format(err))
if schema.tag is not None:
try:
new.tag = validate(schema.tag, value.tag)
except ValueError as err:
raise ValueError("Unable to validate XML tag: {0}".format(err))
if schema.text is not None:
try:
new.text = validate(schema.text, value.text)
except ValueError as err:
raise ValueError("Unable to validate XML text: {0}".format(err))
for child in value:
new.append(child)
return new
示例10: __format_attributes
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def __format_attributes(elt_data):
"""
Private method which converts a single XML tag to a python dict.
It also checks that the elt_data given as argument is of type
xml.etree.ElementTree.Element
:param elt_data: XML Element to be parsed or string
to be converted to a XML Element
:return: Element
"""
rval = {}
if not ET.iselement(elt_data):
raise NmapParserException("Error while trying to parse supplied "
"data attributes: format is not XML or "
"XML tag is empty")
try:
for dkey in elt_data.keys():
rval[dkey] = elt_data.get(dkey)
if rval[dkey] is None:
raise NmapParserException("Error while trying to build-up "
"element attributes: empty "
"attribute {0}".format(dkey))
except:
raise
return rval
示例11: sections
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def sections(self):
""" Returns a list of all :class:`~plexapi.myplex.Section` objects shared with this user.
"""
url = MyPlexAccount.FRIENDSERVERS.format(machineId=self.machineIdentifier, serverId=self.id)
data = self._server.query(url)
sections = []
for section in data.iter('Section'):
if ElementTree.iselement(section):
sections.append(Section(self, section, url))
return sections
示例12: iterateImport
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def iterateImport(self, view, rootModel, rootImport):
if ET.iselement(rootImport):
self.ntab += 1
for childImport in rootImport:
itemType = str(childImport.attrib['type'])
itemTag = str(childImport.tag)
itemText = str(childImport.text)
child0 = qt.QStandardItem(itemTag)
if itemType == "flat":
if rootModel.model() != self.beamModel:
child0 = rootModel.appendRow(child0)
else:
rootModel.appendRow([child0, None, None, None])
elif itemType == "value":
child0 = self.addValue(rootModel, itemTag)
if self.ntab == 1:
self.capitalize(view, child0)
elif itemType == "prop":
child0 = self.addProp(rootModel, itemTag)
elif itemType == "object":
child0, child1 = self.addObject(view,
rootModel,
itemText)
elif itemType == "param":
child0, child1 = self.addParam(rootModel,
itemTag,
itemText)
self.iterateImport(view, child0, childImport)
self.ntab -= 1
else:
pass
示例13: _get_elem
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def _get_elem(elem):
"""Assume an ETree.Element object or a string representation.
Return the ETree.Element object"""
if not ET.iselement(elem):
try:
elem = ET.fromstring(elem)
except:
py2and3.print_("Value Error", elem)
raise ValueError("Cannot convert to element")
return elem
示例14: find
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def find(self, document):
if isinstance(document, basestring):
document = ElementTree.fromstring(document)
elif not ElementTree.iselement(document):
document = ElementTree.parse(document)
point_qname = self._get_qname(self.POINT_CLASS)
lat_qname = self._get_qname(self.LATITUDE_PROPERTY)
long_qname = self._get_qname(self.LONGITUDE_PROPERTY)
alt_qname = self._get_qname(self.ALTITUDE_PROPERTY)
queue = [document]
while queue:
element = queue.pop()
if not self.point_class or element.tag == point_qname:
lat_el = element.find(lat_qname)
long_el = element.find(long_qname)
alt_el = element.find(alt_qname)
if lat_el is not None and long_el is not None:
latitude = lat_el.text
longitude = long_el.text
altitude = alt_el and alt_el.text
try:
point = Point((latitude, longitude, altitude))
except (TypeError, ValueError):
if not self.ignore_invalid:
raise
else:
yield Location(None, point)
queue.extend(reversed(element))
示例15: open
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import iselement [as 别名]
def open(self, string_or_file):
if isinstance(string_or_file, basestring):
string_or_file = ElementTree.fromstring(string_or_file)
elif not ElementTree.iselement(string_or_file):
string_or_file = ElementTree.parse(string_or_file)
if string_or_file.getroot().tag == self._get_qname('gpx'):
self._root = string_or_file.getroot()