简体   繁体   English

python elementtree xml追加

[英]python elementtree xml append

I have some trouble for adding an element to an xml file 我在将一个元素添加到xml文件时遇到了一些麻烦

I have an xml with this structure: 我有一个这种结构的xml:

<Root>
    <Item>
        <ItemId>first</ItemId>
        <Datas>
            <Data>one</Data>
            <Data>two</Data>
            <Data>three</Data>
        </Datas>
    </Item>
    <Item>
        <ItemId>second</ItemId>
        <Datas>
            <Data>one</Data>
            <Data>two</Data>
            <Data>three</Data>
        </Datas>
    </Item>
</Root>

and i want to add data only when the itemid is second, and get an output like this: 并且我想仅在itemid为秒时添加数据,并获得如下输出:

<Root>
    <Item>
        <ItemId>first</ItemId>
        <Datas>
            <Data>one</Data>
            <Data>two</Data>
            <Data>three</Data>
        </Datas>
    </Item>
    <Item>
        <ItemId>second</ItemId>
        <Datas>
            <Data>one</Data>
            <Data>two</Data>
            <Data>three</Data>
            <Data>FOUR</Data>
            <Data>FIVE</Data>
        </Datas>
    </Item>
</Root>

Thanks for your help! 谢谢你的帮助!

It is unclear whether you want how to find where to add the elements or how to add the elements themselves. 目前还不清楚您是否想要如何找到添加元素的位置或如何添加元素本身。

For this specific example, for finding where, you could try something like this: 对于这个具体的例子,为了找到哪里,你可以尝试这样的事情:

import xml.etree.ElementTree as ET
tree=ET.parse('xml-file.txt')
root=tree.getroot()

for item in root.findall('Item'):
    itemid=item.find('ItemId')
    if(itemid.text=='second'):
        #add elements

for the actual adding part, you might try: 对于实际的添加部分,您可以尝试:

new=ET.SubElement(item[1],'Data')
new.text='FOUR'
new=ET.SubElement(item[1],'Data')
new.text='FIVE'

or 要么

new=ET.Element('Data')
new.text='FOUR'
child[1].append(new)
new=ET.Element('Data')
new.text='FIVE'
child[1].append(new)

There are several other ways to do both parts, but, in general, the documentation is very useful: https://docs.python.org/2/library/xml.etree.elementtree.html 还有其他几种方法可以完成这两个部分,但是,一般来说,文档非常有用: https//docs.python.org/2/library/xml.etree.elementtree.html

EDIT: 编辑:

If the "Datas" element is further down, you can use the same Element.find() method as above to find the first occurence of the specified tag. 如果“Datas”元素进一步向下,您可以使用与上面相同的Element.find()方法来查找指定标记的第一个出现。 (Element.findall() returns a list of all occurences of the specified tag). (Element.findall()返回指定标记的所有出现的列表)。

The following should do the trick: 以下应该做的伎俩:

data=item.find('Datas')
new=ET.SubElement(data,'Data')
new.text='FOUR'
new=ET.SubElement(data,'Data')
new.text='FIVE'

Following way you can find the Datas node and append element to it. 按照以下方式,您可以找到Datas节点并向其追加元素。

from lxml import etree
from xml.etree import ElementTree as ET

xml_str = """<Root>
<Item>
    <ItemId>first</ItemId>
    <Datas>
        <Data>one</Data>
        <Data>two</Data>
        <Data>three</Data>
    </Datas>
</Item>
<Item>
    <ItemId>second</ItemId>
    <Datas>
        <Data>one</Data>
        <Data>two</Data>
        <Data>three</Data>
    </Datas>
</Item>
</Root>"""

# build the tree 
tree = etree.fromstring(xml_str)
# get all items nodes 
items = tree.findall('Item')

for item in items:
    # get ItemId text 
    item_id = item.findtext('ItemId')
    if item_id == 'second':
        # get the Datas node
        datas = item.find('Datas')

        # add an element to it
        new_data = ET.SubElement(datas, 'Data')
        new_data.text = 'New Data'

# print the final xml tree 
print etree.tostring(tree)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM