Python xml.etree.ElementTree 问题

韦斯蒂

没关系 - 我发现了我真正的问题,我意识到它在我的代码中更进一步。

我在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)))

我想弄清楚为什么我找不到destinationsroot

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'

如果我在 get 之后添加以下代码,我会root看到日志中列出的每个目的地:

for destination in root:
  destinationList.append(destination)
  logging.debug('DIAG: %s: destination.tag = %s'
    % (FUNCTION_NAME, destination.tag))

同样的代码在不同的脚本中工作,所以我不确定为什么它在这里不起作用。

纳伦德拉乔杜里

你得到None是因为ET.dump写入sys.stdout并且你正在记录它的返回dumpNone.

文档

xml.etree.ElementTree.dump(elem)

将元素树或元素结构写入 sys.stdout。此功能应仅用于调试。

确切的输出格式取决于实现。在这个版本中,它被写成一个普通的 XML 文件。

elem 是元素树或单个元素。

尝试使用tostringmethod 而不是dump.

logging.debug("DIAG: %s: root = %r" % (FUNCTION_NAME, ET.tostring(root)))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 Python xml.etree.ElementTree 遍历 XML 树的问题

来自分类Dev

Python xml.etree.ElementTree问题,Element.tag不仅给我带来了标签名,

来自分类Dev

Python xml.etree 问题

来自分类Dev

python(xml.etree.ElementTree)中的XML解析

来自分类Dev

python xml.etree.ElementTree追加到子元素

来自分类Dev

xml.etree.ElementTree getElementByID()?

来自分类Dev

xml.etree.ElementTree findall

来自分类Dev

xml.etree.ElementTree findall

来自分类Dev

从Python中的XML数据创建字典(使用xml.etree.ElementTree)

来自分类Dev

使用xml.etree.ElementTree的Python xml解析不起作用

来自分类Dev

xml.etree.ElementTree-麻烦设置xmlns ='...'

来自分类Dev

如何在python xml.etree.ElementTree中的迭代器中删除节点

来自分类Dev

Python:xml.etree.ElementTree.ParseError:格式不正确(无效的令牌)

来自分类Dev

在Python中,如何使用xml.etree.ElementTree创建数据框?

来自分类Dev

xml.etree.ElementTree >> Python >>如何访问子元素并进行断言

来自分类Dev

如何使用xml.etree.ElementTree Python格式化属性,前缀和标签

来自分类Dev

python xml.etree.ElementTree tostring()fromstring()往返对unicode代码点失败

来自分类Dev

如何使用python(xml.etree.ElementTree)解决下一个迭代?

来自分类Dev

Python - 可以重命名 xml.etree.ElementTree.iter() 以实现 2.6 向后兼容性吗?

来自分类Dev

如何使用 python 从soap响应中的多个命名空间获取数据:xml.etree.ElementTree

来自分类Dev

python elementTree XML解析器的性能问题

来自分类Dev

使用xml.etree.ElementTree XML解析子元素

来自分类Dev

使用xml.etree.ElementTree搜索XML元素树的属性

来自分类Dev

使用xml.etree.ElementTree(python)解析XML时,如何区分常规空格和转义空格(&#32;)

来自分类Dev

使用Python模块xml.etree.ElementTree解析有点复杂的XML并将值存储在List中

来自分类Dev

xml.etree.ElementTree iterparse()仍在使用大量内存?

来自分类Dev

使用html5lib和xml.etree.ElementTree

来自分类Dev

如何使用xml.etree.ElementTree访问标签之间的文本

来自分类Dev

IronPython和xml.etree.ElementTree非法字符错误

Related 相关文章

  1. 1

    使用 Python xml.etree.ElementTree 遍历 XML 树的问题

  2. 2

    Python xml.etree.ElementTree问题,Element.tag不仅给我带来了标签名,

  3. 3

    Python xml.etree 问题

  4. 4

    python(xml.etree.ElementTree)中的XML解析

  5. 5

    python xml.etree.ElementTree追加到子元素

  6. 6

    xml.etree.ElementTree getElementByID()?

  7. 7

    xml.etree.ElementTree findall

  8. 8

    xml.etree.ElementTree findall

  9. 9

    从Python中的XML数据创建字典(使用xml.etree.ElementTree)

  10. 10

    使用xml.etree.ElementTree的Python xml解析不起作用

  11. 11

    xml.etree.ElementTree-麻烦设置xmlns ='...'

  12. 12

    如何在python xml.etree.ElementTree中的迭代器中删除节点

  13. 13

    Python:xml.etree.ElementTree.ParseError:格式不正确(无效的令牌)

  14. 14

    在Python中,如何使用xml.etree.ElementTree创建数据框?

  15. 15

    xml.etree.ElementTree >> Python >>如何访问子元素并进行断言

  16. 16

    如何使用xml.etree.ElementTree Python格式化属性,前缀和标签

  17. 17

    python xml.etree.ElementTree tostring()fromstring()往返对unicode代码点失败

  18. 18

    如何使用python(xml.etree.ElementTree)解决下一个迭代?

  19. 19

    Python - 可以重命名 xml.etree.ElementTree.iter() 以实现 2.6 向后兼容性吗?

  20. 20

    如何使用 python 从soap响应中的多个命名空间获取数据:xml.etree.ElementTree

  21. 21

    python elementTree XML解析器的性能问题

  22. 22

    使用xml.etree.ElementTree XML解析子元素

  23. 23

    使用xml.etree.ElementTree搜索XML元素树的属性

  24. 24

    使用xml.etree.ElementTree(python)解析XML时,如何区分常规空格和转义空格(&#32;)

  25. 25

    使用Python模块xml.etree.ElementTree解析有点复杂的XML并将值存储在List中

  26. 26

    xml.etree.ElementTree iterparse()仍在使用大量内存?

  27. 27

    使用html5lib和xml.etree.ElementTree

  28. 28

    如何使用xml.etree.ElementTree访问标签之间的文本

  29. 29

    IronPython和xml.etree.ElementTree非法字符错误

热门标签

归档