0

次のコード (または、それに非常によく似たコード) は、1 日中私の頭を悩ませています。私はアイデアがありません。

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)

両方の子が同じ修飾名を持っている場合、要素に 2 つの子を追加できません。これを回避する方法はありますか (明日までに xml ライターを交換するには遅すぎるため、etree を使用します)。

4

0 に答える 0