gpt4 book ai didi

python - 使用 lxml 重复 XML 元素

转载 作者:行者123 更新时间:2023-12-01 00:27:57 24 4
gpt4 key购买 nike

我必须创建一个 XML 文档,该文档必须在不同部分重复相同的信息。我正在创建一些 details 作为 etree.Element 并尝试将其附加到几个辅助 XML 元素

from lxml import etree

top = etree.Element('Primary')
element1 = etree.Element('Secondary')
element2 = etree.Element('Secondary')

details = etree.Element('Details', somevalue='value')

element1.append(details)
element2.append(details)
top.append(element1)
top.append(element2)

print(etree.tostring(top, encoding="unicode", pretty_print=True))

我想要的输出是;

<Primary>
<Secondary>
<Details somevalue="value"/>
</Secondary>
<Secondary>
<Details somevalue="value"/>
</Secondary>
</Primary>

我得到的输出是;

<Primary>
<Secondary/>
<Secondary>
<Details somevalue="value"/>
</Secondary>
</Primary>

lxml 似乎只允许在一个地方使用 details 元素。有什么办法可以关掉这个吗?

谢谢!

最佳答案

以下内容摘自lxml documentation

Note that in the original ElementTree, a single Element object can sit in any number of places in any number of trees, which allows for the same copy operation as with lists. The obvious drawback is that modifications to such an Element will apply to all places where it appears in a tree, which may or may not be intended.

The upside of this difference is that an Element in lxml.etree always has exactly one parent, which can be queried through the getparent() method. This is not supported in the original ElementTree.

因此,lxml 中的元素只能有一个父元素,这与 ElementTree 中的原始元素不同。因此不可能使用 lxml 将相同的元素附加到多个父元素。然而,文档建议处理这个新设计,您应该使用深层复制来复制要附加到另一个元素(如果它已经分配到某个地方)。

这将创建元素的新副本,因此可以将新副本分配给不同的父元素。这些是单独的副本,因此更改一个副本不会更改另一个副本。

from lxml import etree
from copy import deepcopy

top = etree.Element('Primary')
element1 = etree.Element('Secondary')
element2 = etree.Element('Secondary')
details = etree.Element('Details', somevalue='value')
element1.append(details)
element2.append(deepcopy(details))
top.append(element1)
top.append(element2)
print(etree.tostring(top, encoding="unicode", pretty_print=True))

输出

<Primary>
<Secondary>
<Details somevalue="value"/>
</Secondary>
<Secondary>
<Details somevalue="value"/>
</Secondary>
</Primary>

更新了 Jack 的示例

因此,在 jack 的示例中,我设置了一个变量num_secondarys,它将创建 X 个辅助元素,每个元素都有一个详细信息元素,并且详细信息元素中的值将递增 1。我创建了一个辅助元素和详细信息作为模板,然后使用深度复制将它们复制为新元素。

from lxml import etree
from copy import deepcopy

top = etree.Element('Primary')
secondary = etree.Element('Secondary')
detail = etree.Element('Details', somevalue='value')
num_secondarys = 3
for i in range(1, num_secondarys + 1):
this_secondary = deepcopy(secondary)
this_detail = deepcopy(detail)
this_detail.attrib['somevalue']+=str(i)
this_secondary.append(this_detail)
top.append(this_secondary)

print(etree.tostring(top, encoding="unicode", pretty_print=True))

输出

<Primary>
<Secondary>
<Details somevalue="value1"/>
</Secondary>
<Secondary>
<Details somevalue="value2"/>
</Secondary>
<Secondary>
<Details somevalue="value3"/>
</Secondary>
</Primary>

关于python - 使用 lxml 重复 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58408621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com