НИКОГДА НЕ ЗАНИМАЙТЕСЬ - Я НАШЕЛ СВОЮ НАСТОЯЩУЮ ПРОБЛЕМУ, ЭТО БЫЛО ДАЛЬШЕ В МОЕМ КОДЕ, КОТОРЫЙ Я РЕАЛИЗИЛ.
У меня проблемы с тем, чтобы xml.etree.ElementTree работал так, как я ожидал.
xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><suggestedmatches><destination><sortOrder>1</sortOrder><destinationType>destinationType1</destinationType></destination><destination><sortOrder>2</sortOrder><destinationType>destinationType2</destinationType></destination></suggestedmatches>"
root = ET.fromstring(xmlData)
logging.debug("DIAG: %s: root.tag = %s"
% (FUNCTION_NAME, root.tag))
logging.debug("DIAG: %s: root = %r" % (FUNCTION_NAME, ET.tostring(root)))
destinations = root.findall("destination")
logging.debug('DIAG: %s: destinations = %r' % (FUNCTION_NAME, ET.tostring(destinations)))
Пытаюсь понять, почему не могу найти destinations в root.
DEBUG:root:DIAG: findDestinations(): root.tag = suggestedmatches
DEBUG:root:DIAG: findDestinations(): root = b'<suggestedmatches><destination><sortOrder>1</sortOrder><destinationType>destinationType1</destinationType></destination><destination><sortOrder>2</sortOrder><destinationType>destinationType2</destinationType></destination></suggestedmatches>'
ERROR:root:findDestinations(): Encountered exception on root.findall() - 'list' object has no attribute 'iter'
И если я добавлю следующий код после получения root, я увижу все пункты назначения, перечисленные в журнале:
for destination in root:
destinationList.append(destination)
logging.debug('DIAG: %s: destination.tag = %s'
% (FUNCTION_NAME, destination.tag))
Этот же код работает в другом скрипте, поэтому я не уверен, почему он здесь не работает.
Понятно! Это псевдоним импорта для ElementTree.






Вы получаете None, потому что ET.dump записывает в sys.stdout, и вы регистрируете возврат dump, который является None.
От документы:
xml.etree.ElementTree.dump(elem)
Writes an element tree or element structure to sys.stdout. This function should be used for debugging only.
The exact output format is implementation dependent. In this version, it’s written as an ordinary XML file.
elem is an element tree or an individual element.
Попробуйте использовать метод tostring вместо dump.
logging.debug("DIAG: %s: root = %r" % (FUNCTION_NAME, ET.tostring(root)))
Спасибо, это объясняет странность, которую я увидел в выводе. Пожалуйста, просмотрите мои обновления по моему вопросу выше, проблема в том, что я не могу извлечь данные ..
Что здесь
ET? Это встроенный класс? Или это из какого-то стороннего пакета?