0

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

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编写器为时已晚)?

4

0 回答 0