stchris / untangle

Converts XML to Python objects
MIT License
611 stars 83 forks source link

Unable to retrieve parsed xml in list "RecursionError('maximum recursion depth exceeded while calling a Python object',) #55

Closed lolcats4u closed 2 years ago

lolcats4u commented 6 years ago

I am trying to parse a list of xml files using the following code

import untangle

most_recent_inventory_string_split = most_recent_inventory_report_list_xml.split("<ReportInfo>")
middle_tag = "<ReportInfo>"
header = most_recent_inventory_string_split[0] +middle_tag
end_tags = " </GetReportListResult> \n </GetReportListResponse>"
most_recent_inventory_string_xml_cleaned = header + most_recent_inventory_string_split[1] + end_tags

xml_list = []

for i in most_recent_inventory_string_split:
    xml_list.append(header + i + end_tags) 

xml_list[-1] = header + most_recent_inventory_string_split[-1]
xml_list[0] = most_recent_inventory_string_split[0] + end_tags

parsed_xml_list = []

for i in xml_list:
    parsed_xml_list.append(untangle.parse(i))

Where the original xml string -> most_recent_inventory_report_list_xml looks like this censored for privacy reasons

<?xml version="1.0"?>
<GetReportListResponse xmlns="http://mws.amazonaws.com">
  <GetReportListResult>
    <HasNext>true</HasNext>
    <NextToken>bleh</NextToken>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-07T09:31:18+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-06T10:18:16+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T23:43:06+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T23:32:28+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T23:25:51+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T23:09:34+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T23:00:17+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T22:49:30+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T22:39:37+00:00</AvailableDate>
    </ReportInfo>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-05T22:23:19+00:00</AvailableDate>
    </ReportInfo>
  </GetReportListResult>
  <ResponseMetadata>
    <RequestId>numbers_and_letters</RequestId>
  </ResponseMetadata>
</GetReportListResponse>

at the end of the day, every element in the xml_list looks similar to this, and the list has 11 of these elements

<?xml version="1.0"?>
<GetReportListResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
  <GetReportListResult>
    <HasNext>true</HasNext>
    <NextToken>bleh</NextToken>
    <ReportInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_DATA_</ReportType>
      <Acknowledged>false</Acknowledged>
      <ReportId>numbers</ReportId>
      <ReportRequestId>numbers</ReportRequestId>
      <AvailableDate>2018-03-07T09:31:18+00:00</AvailableDate>
    </ReportInfo>
     </GetReportListResult> 
 </GetReportListResponse>

and I have had a very hard time looking up things in the ending list which should have the same number of elements, but now be made of now-parsed xml. I had originally gone about doing the list idea because of a similar recursion depth problem, thinking it was because of the repetitive nature of the tags. However I am not sure that this one is me anymore.

stchris commented 2 years ago

Unsure what to do here. Please reopen if there is a concrete problem with untangle.