text
stringlengths
0
828
_ELEM_ATTRIBS: element.attrib,
_ELEM_CHILDREN: []
}
if recurse is True:
for child in element:
converted[_ELEM_CHILDREN].append(element_to_dict(child, recurse=recurse))
return converted
return {}"
4044,"def element_to_object(elem_to_parse, element_path=None):
""""""
:return: the root key, and a dict with all the XML data, but without preserving structure, for instance:
<elem val=""attribute""><val>nested text</val><val prop=""attr"">nested dict text</val>nested dict tail</elem>
{'elem': {
'val': [
u'nested text',
{'prop': u'attr', 'value': [u'nested dict text', u'nested dict tail']},
u'attribute'
]
}}
""""""
if isinstance(elem_to_parse, STRING_TYPES) or hasattr(elem_to_parse, 'read'):
# Always strip namespaces if not already parsed
elem_to_parse = strip_namespaces(elem_to_parse)
if element_path is not None:
elem_to_parse = get_element(elem_to_parse, element_path)
element_tree = get_element_tree(elem_to_parse)
element_root = element_tree.getroot()
root_tag = u'' if element_root is None else element_root.tag
return root_tag, {root_tag: _element_to_object(element_root)}"
4045,"def element_to_string(element, include_declaration=True, encoding=DEFAULT_ENCODING, method='xml'):
"""""" :return: the string value of the element or element tree """"""
if isinstance(element, ElementTree):
element = element.getroot()
elif not isinstance(element, ElementType):
element = get_element(element)
if element is None:
return u''
element_as_string = tostring(element, encoding, method).decode(encoding=encoding)
if include_declaration:
return element_as_string
else:
return strip_xml_declaration(element_as_string)"
4046,"def string_to_element(element_as_string, include_namespaces=False):
"""""" :return: an element parsed from a string value, or the element as is if already parsed """"""
if element_as_string is None:
return None
elif isinstance(element_as_string, ElementTree):
return element_as_string.getroot()
elif isinstance(element_as_string, ElementType):
return element_as_string
else:
element_as_string = _xml_content_to_string(element_as_string)
if not isinstance(element_as_string, string_types):
# Let cElementTree handle the error
return fromstring(element_as_string)
elif not strip_xml_declaration(element_as_string):
# Same as ElementTree().getroot()
return None
elif include_namespaces:
return fromstring(element_as_string)
else:
return fromstring(strip_namespaces(element_as_string))"
4047,"def iter_elements(element_function, parent_to_parse, **kwargs):
""""""
Applies element_function to each of the sub-elements in parent_to_parse.
The passed in function must take at least one element, and an optional
list of kwargs which are relevant to each of the elements in the list:
def elem_func(each_elem, **kwargs)
""""""
parent = get_element(parent_to_parse)
if not hasattr(element_function, '__call__'):
return parent
for child in ([] if parent is None else parent):
element_function(child, **kwargs)
return parent"
4048,"def iterparse_elements(element_function, file_or_path, **kwargs):
""""""
Applies element_function to each of the sub-elements in the XML file.
The passed in function must take at least one element, and an optional
list of **kwarg which are relevant to each of the elements in the list:
def elem_func(each_elem, **kwargs)
Implements the recommended cElementTree iterparse pattern, which is