如何向元素添加 xml 片段

How to add a xml fragment to an element

我正在从 Amara (http://xml3k.org/Amara/Tutorial) 转换为 lxml,在 Amara 中我可以这样做:

for wxp in self.Points:
        points.xml_append_fragment('<point><x>%i</x><y>%i</y></point>' % (wxp[0], wxp[1]))

其中 'points' 是一个元素,我如何用 lxml.objectify 做到这一点?

找到了。

for wxp in self.Points:
            #points.xml_append_fragment('<point><x>%i</x><y>%i</y></point>' % (wxp[0], wxp[1]))
            point = xmlu.addElement(points, u'point', ns=None)
            xmlu.addElementPlusValue(point, u'x', wxp[0])
            xmlu.addElementPlusValue(point, u'y', wxp[1])

'addElement' 基本上是在做 objectify.SubElement 而 'addElementPlusValue' 是在做 setattr(element, name, value)