以下代码(或者更确切地说,代码非常类似)一整天都在努力。我没有想法。
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编写器为时已晚)?