使用 lxml 将多个元素添加到 xml
Add multiple elements to xml with lxml
我想在 xml 树的父元素中添加一些子元素。
来源 XML 是:
<catalog>
<element>
<collection_list />
</element>
<element>
<collection_list />
</element>
</catalog>
我试过 python 3 中的以下代码:
from lxml import etree
tree = etree.parse('source.xml')
root = tree.getroot()
elementCollections = tree.xpath('/catalog/element/collection_list')
for element in elementCollections:
childElement = etree.SubElement(element, "collection")
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement.text = elementCollection
newtree = etree.tostring(tree, encoding='utf-8')
newtree = newtree.decode("utf-8")
print(newtree)
但不是:
<catalog>
<element>
<collection_list>
<collection>c1</collection>
<collection>c2</collection>
<collection>c3</collection>
</collection_list>
</element>
<element>
<collection_list>
<collection>c1</collection>
<collection>c2</collection>
<collection>c3</collection>
</collection_list>
</element>
</catalog>
我有这样的结果:
<catalog>
<element>
<collection_list>
<collection>c3</collection>
</collection_list>
</element>
<element>
<collection_list>
<collection>c3</collection>
</collection_list>
</element>
</catalog>
请解释一下如何在树中插入多个元素。
编辑:
"childElement".
的固定标签名称
使用此代码:
from lxml import etree
tree = etree.parse('source.xml')
root = tree.getroot()
elementCollections = tree.xpath('/catalog/element/collection_list')
for element in elementCollections:
childElement = etree.SubElement(element, "collectionName")
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement2 = etree.SubElement(childElement, "collection")
childElement2.text = elementCollection
newtree = etree.tostring(tree, encoding='utf-8')
newtree = newtree.decode("utf-8")
print(newtree)
您需要在循环中创建新元素。
您需要在内部 for 循环而不是外部循环中添加集合:
for element in elementCollections:
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement = etree.SubElement(element, "collection")
childElement.text = elementCollection
newtree = etree.tostring(root, encoding='utf-8')
newtree = newtree.decode("utf-8")
print newtree
我想在 xml 树的父元素中添加一些子元素。 来源 XML 是:
<catalog>
<element>
<collection_list />
</element>
<element>
<collection_list />
</element>
</catalog>
我试过 python 3 中的以下代码:
from lxml import etree
tree = etree.parse('source.xml')
root = tree.getroot()
elementCollections = tree.xpath('/catalog/element/collection_list')
for element in elementCollections:
childElement = etree.SubElement(element, "collection")
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement.text = elementCollection
newtree = etree.tostring(tree, encoding='utf-8')
newtree = newtree.decode("utf-8")
print(newtree)
但不是:
<catalog>
<element>
<collection_list>
<collection>c1</collection>
<collection>c2</collection>
<collection>c3</collection>
</collection_list>
</element>
<element>
<collection_list>
<collection>c1</collection>
<collection>c2</collection>
<collection>c3</collection>
</collection_list>
</element>
</catalog>
我有这样的结果:
<catalog>
<element>
<collection_list>
<collection>c3</collection>
</collection_list>
</element>
<element>
<collection_list>
<collection>c3</collection>
</collection_list>
</element>
</catalog>
请解释一下如何在树中插入多个元素。
编辑: "childElement".
的固定标签名称使用此代码:
from lxml import etree
tree = etree.parse('source.xml')
root = tree.getroot()
elementCollections = tree.xpath('/catalog/element/collection_list')
for element in elementCollections:
childElement = etree.SubElement(element, "collectionName")
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement2 = etree.SubElement(childElement, "collection")
childElement2.text = elementCollection
newtree = etree.tostring(tree, encoding='utf-8')
newtree = newtree.decode("utf-8")
print(newtree)
您需要在循环中创建新元素。
您需要在内部 for 循环而不是外部循环中添加集合:
for element in elementCollections:
listOfElementCollections = ['c1', 'c2', 'c3']
for elementCollection in listOfElementCollections:
childElement = etree.SubElement(element, "collection")
childElement.text = elementCollection
newtree = etree.tostring(root, encoding='utf-8')
newtree = newtree.decode("utf-8")
print newtree