【问题标题】:python elementtree xml appendpython elementtree xml追加
【发布时间】:2015-10-16 08:46:25
【问题描述】:

我在向 xml 文件中添加元素时遇到了一些麻烦

我有一个具有这种结构的 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>

我只想在 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>

感谢您的帮助!

【问题讨论】:

    标签: python xml parsing elementtree


    【解决方案1】:

    不清楚您是想如何找到添加元素的位置还是如何添加元素本身。

    对于这个特定的例子,为了找到哪里,你可以尝试这样的事情:

    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
    

    对于实际添加部分,您可以尝试:

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

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

    这两个部分还有其他几种方法,但总的来说,文档非常有用:https://docs.python.org/2/library/xml.etree.elementtree.html

    编辑:

    如果“Datas”元素再往下,可以使用与上面相同的 Element.find() 方法来查找指定标签的第一次出现。 (Element.findall() 返回指定标签的所有出现的列表)。

    以下应该可以解决问题:

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

    【讨论】:

    • 非常感谢,您的解决方案正在运行,但我还有另一个问题:如果“”和“”之间有另一个标签,它会停止工作,我怎样才能找到数据节点并附加另一个数据?
    • @SergiX44 我编辑了答案以包含该信息。只需使用上面使用的 Element.find() 方法来查找您想要的标签的第一次出现。
    【解决方案2】:

    您可以通过以下方式找到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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多