我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用xml.etree.ElementTree.iselement()。
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))
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
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))
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
def annotate_with_XMLNS(tree, prefix, URI): """ Annotates the provided DOM tree with XMLNS attributes and adds XMLNS prefixes to the tags of the tree nodes. :param tree: the input DOM tree :type tree: an ``xml.etree.ElementTree.ElementTree`` or ``xml.etree.ElementTree.Element`` object :param prefix: XMLNS prefix for tree nodes' tags :type prefix: str :param URI: the URI for the XMLNS definition file :type URI: str """ if not ET.iselement(tree): tree = tree.getroot() tree.attrib['xmlns:' + prefix] = URI iterator = tree.iter() next(iterator) # Don't add XMLNS prefix to the root node for e in iterator: e.tag = prefix + ":" + e.tag
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
def build_xml(request_params, type): post_data = ET.Element(type) for k, v in request_params.items(): if v is None: v = "" if ET.iselement(v): post_data.append(v) elif isinstance(v, str): ET.SubElement(post_data, k).text = unicode_convert(v) elif isinstance(v, int): ET.SubElement(post_data, k).text = str(v) else: ET.SubElement(post_data, k).text = v post_data = ('<?xml version="1.0" encoding="UTF-8"?>\n%s' % ET.tostring(post_data)) return post_data
def set_prefixes(self, elem, prefix_map): # check if this is a tree wrapper if not ElementTree.iselement(elem): elem = elem.getroot() # build uri map and add to root element uri_map = {} for prefix, uri in prefix_map.items(): uri_map[uri] = prefix elem.set("xmlns:" + prefix, uri) # fixup all elements in the tree memo = {} for elem in elem.getiterator(): self.fixup_element_prefixes(elem, uri_map, memo)
def editElement(self, element, values={}): if et.iselement(element): for vkey in values.keys(): if element.attrib.has_key(vkey) and values[vkey]==None: del element.attrib[vkey] else: element.attrib[vkey] = values[vkey] return True else: return False
def deleteElement(self, element, parent=None): if et.iselement(element): if parent==None: parent = self.get_parent(element) if et.iselement(parent): if parent.tag in("row","datagrid"): parent = parent.getiterator("columns")[0] parent.remove(element) return True
def import_epf(epf): """Import an EPF file. :param epf: Either a path to an EPF-file, a file-like object, or an instance of :class:`xml.etree.ElementTree.Element`. :returns: The Object Dictionary. :rtype: canopen.ObjectDictionary """ od = objectdictionary.ObjectDictionary() if etree.iselement(epf): tree = epf else: tree = etree.parse(epf).getroot() # Find and set default bitrate can_config = tree.find("Configuration/CANopen") if can_config is not None: bitrate = can_config.get("BitRate", "250") bitrate = bitrate.replace("U", "") od.bitrate = int(bitrate) * 1000 # Parse Object Dictionary for group_tree in tree.iterfind("Dictionary/Parameters/Group"): name = group_tree.get("SymbolName") parameters = group_tree.findall("Parameter") index = int(parameters[0].get("Index"), 0) if len(parameters) == 1: # Simple variable var = build_variable(parameters[0]) # Use top level index name instead var.name = name od.add_object(var) elif len(parameters) == 2 and parameters[1].get("ObjectType") == "ARRAY": # Array arr = objectdictionary.Array(name, index) for par_tree in parameters: var = build_variable(par_tree) arr.add_member(var) description = group_tree.find("Description") if description is not None: arr.description = description.text od.add_object(arr) else: # Complex record record = objectdictionary.Record(name, index) for par_tree in parameters: var = build_variable(par_tree) record.add_member(var) description = group_tree.find("Description") if description is not None: record.description = description.text od.add_object(record) return od