

def parse_listdelimited_aws_xml(xml_stream, list_tags=None):
    "\n    Parse a stream of XML and return a nested dict.  The dict represents each\n    XML element with a key that matches the element's name and a value of\n    another dict if the element contains at least one child element, or the\n    element's text if it does not.\n\n    For each element whose name appears in the list_tags list, its dict\n    value will instead be a list that aggregates the values of each of that\n    element's children.\n\n    This function is designed to parse XML from AWS services that explicitly\n    start and end lists with their own elements, such as EC2.\n\n    Examples:\n        <a><b>spam</b><c>eggs</c></a> -> {'a': {'b': 'spam', 'c': 'eggs'}}\n\n        <a>\n          <b>\n            <c>spam</c>\n          </b>          + ['a'] -> {'a': [{'c': 'spam'}, {'c': 'eggs'}]}\n          <b>\n            <c>eggs</c>\n          </b>\n        </a>\n    "
    if (list_tags is None):
        list_tags = ()
    stack = [(None, {
        
    })]
    try:
        for (event, elem) in ElementTree.iterparse(xml_stream, events=('start', 'end')):
            tag = _strip_ns(elem.tag)
            if (event[0] == 's'):
                if (tag in list_tags):
                    stack.append((tag, []))
                else:
                    stack.append((tag, {
                        
                    }))
            else:
                assert (tag == elem[(- 1)][0])
                if isinstance(stack[(- 2)][1], list):
                    if (stack[(- 1)][1] == {
                        
                    }):
                        if (elem.text is not None):
                            stack[(- 2)][1].append(elem.text)
                        else:
                            stack[(- 2)][1].append({
                                
                            })
                    else:
                        stack[(- 2)][1].append(stack[(- 1)][1])
                elif (stack[(- 1)][1] == {
                    
                }):
                    if (elem.text is not None):
                        stack[(- 2)][1][tag] = elem.text
                    else:
                        stack[(- 2)][1][tag] = {
                            
                        }
                else:
                    stack[(- 2)][1][tag] = stack[(- 1)][1]
                stack.pop()
                elem.clear()
    except (ParseError, SyntaxError):
        raise ValueError('XML parse error')
    return stack[0][1]

dataset/ETHPy150Open boto/requestbuilder/requestbuilder/xmlparse.py parse_listdelimited_aws_xml/VarMisuse@143/541 `stack`->`elem`