元素子元素上的标记

2024-09-29 21:36:34 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码(或者更确切地说,非常类似的代码)一整天都在困扰着我。我没主意了。在

import xml.etree.ElementTree as etree

parent = etree.Element(etree.QName('http://www.example.com', tag='parent'))
child_a = etree.Element(etree.QName('http://www.example.com', tag='child'))
child_a.text='Bill'
parent.append(child_a)
child_b = etree.Element(etree.QName('http://www.example.com', tag='child'))
child_b.text='Barry'
parent.append(child_b)
print(etree.tostring(parent))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "Lib\xml\etree\ElementTree.py", line 1120, in tostring
  File "Lib\xml\etree\ElementTree.py", line 812, in write
  File "Lib\xml\etree\ElementTree.py", line 880, in namespaces
  File "Lib\xml\etree\ElementTree.py", line 1046, in _raise_serialization_error
TypeError: cannot serialize <xml.etree.ElementTree.QName object at 0x0000000222B2198> (type QName)

如果两个子元素具有相同的限定名,则不允许我向元素中添加两个子元素。有什么办法可以解决这个问题(使用etree,因为在明天之前替换掉我的xml writer已经太晚了)?在


Tags: inpychildhttpexamplelibwwwline

热门问题